The following code is a simple react component that uses react-spring to animate the p
(animated.p
) elements. The animation is defined in the fadeIn
constant and is assigned to the animated.p
elements via the style={fadeIn}
tags.
//returns and animates <p> elements:
function QuoteBox(props) {
const fadeIn = useSpring({ opacity: 1, from: { opacity: 0 } });
return (
<>
<blockquote>
<animated.p id="text" style={fadeIn}>{props.quote}</animated.p>
</blockquote>
<animated.p id="author" style={fadeIn}>{props.author}<cite><em>{props.from}</em></cite></animated.p>
</>
);
}
When the page loads and the QuoteBox
component is mounted into the DOM, useSpring
changes the opacity of the animated.p
elements from 0 to 1.
However, I need this to happen every time I pass new props
to the animated.p
elements, but I'm struggling to understand how to do that.
There are some simple examples on the react-spring docs page, which uses the state
(toggle
) of the component to change the opacity from 0 to 1 and visa-versa, but it's not clear to me how to change the opacity from 0 to 1 every time I pass a new prop
to the animated.p
elements.
I'm just trying to obtain a simmple text fade-in when it changes.
Sorry for the previous confused question, this may be a little too difficult for me. Also, I'm not entirely sure react-spring is a good choice, it seems overly complicated for what I'm trying to achieve.