I have a react function component that needs to use a framer motion hook to build up the props of an object called styles, however you can't use hooks conditionally, so I'd like to know how to use hooks conditionally?
let styles = {};
if (scaleUp) {
styles.scale = useTransform(scrollYProgress, [0, 1], [1, 1.2]);
}
if (scaleDown) {
styles.scale = useTransform(scrollYProgress, [0, 1], [1.2, 1]);
}
if (fadeIn) {
styles.opacity = useTransform(scrollYProgress, [0, 1], [0, 1]);
}
if (fadeOut) {
styles.opacity = useTransform(scrollYProgress, [0, 1], [1, 0]);
}
I have a react function component that needs to use a framer motion hook to build up the props of an object called styles, however you can't use hooks conditionally, so I'd like to know how to use hooks conditionally?
let styles = {};
if (scaleUp) {
styles.scale = useTransform(scrollYProgress, [0, 1], [1, 1.2]);
}
if (scaleDown) {
styles.scale = useTransform(scrollYProgress, [0, 1], [1.2, 1]);
}
if (fadeIn) {
styles.opacity = useTransform(scrollYProgress, [0, 1], [0, 1]);
}
if (fadeOut) {
styles.opacity = useTransform(scrollYProgress, [0, 1], [1, 0]);
}
You can't conditionally call React hooks, you'll just need to unconditionally call each hook and apply the appropriate return values.
Example:
const scaleUp = useTransform(scrollYProgress, [0, 1], [1, 1.2]);
const scaleDown = useTransform(scrollYProgress, [0, 1], [1.2, 1]);
const fadeIn = useTransform(scrollYProgress, [0, 1], [0, 1]);
const fadeOut = useTransform(scrollYProgress, [0, 1], [1, 0]);
const styles = {};
if (effects.scaleUp) {
styles.scale = scaleUp;
}
if (effects.scaleDown) {
styles.scale = scaleDown;
}
if (effects.fadeIn) {
styles.opacity = fadeIn;
}
if (effects.fadeOut) {
styles.opacity = fadeOut;
}