I am using motion to animate a background color whenever the variable colorHex
changes, which works nicely. I would also like to scale up and back down each time the color changes. For this I’ve used scale: [1, 2, 1]
however because the value never changes, it only runs on the initial animation. How can I ensure it retriggers whenever colorHex
changes?
<motion.div
transition={{ duration: 0.3, delay: offset * 0.1, ease: "easeOut" }}
animate={{
backgroundColor: colorHex,
scale: [1, 2, 1],
}}
...
Note that the only work around I’ve found is to set the scale to a new (very slightly different) value when the color value changes.
2
Answers
All you need to do is add a key prop.
First, you can trigger "manual" animations by using
useAnimate
. Please note that I modified your scale to[1,2,3,1]
, because potentially, you would like to go back to original scale:Now that you have control on how to trigger the animation, you can just use
React.useEffect
to listen for changes on yourcolorHex
:Here is a small repro with the full code.