To create animations in React with the react-spring library, follow these steps:
npm install react-spring
import { useSpring, animated } from 'react-spring';
useSpring
hook. This hook allows you to create animated values that can be interpolated and used to update styles:const props = useSpring({
opacity: 1,
from: { opacity: 0 },
})
animated
higher-order component:<animated.div style={props}>Hello, React Spring!</animated.div>
useSpring
hook:const props = useSpring({
opacity: 1,
transform: 'scale(1)',
color: 'black',
from: { opacity: 0, transform: 'scale(0)', color: 'white' },
})
useSpring
hook in combination with event handlers:const handleButtonClick = () => {
setProps({
opacity: 0,
transform: 'scale(0)',
})
}
<button onClick={handleButtonClick}>Animate</button>
By following these steps, you can easily create animations in React using the react-spring library.