To add animations with the react-transition-group library, follow these steps:
npm install react-transition-group
import { CSSTransition } from 'react-transition-group';
CSSTransition
component and specify the CSS classes for the entering and exiting states:<CSSTransition
in={isShowing}
timeout={200}
classNames="fade"
unmountOnExit
>
<div className="box">Content to animate</div>
</CSSTransition>
.fade-enter {
opacity: 0;
}
.fade-enter-active {
opacity: 1;
transition: opacity 200ms ease-in;
}
.fade-exit {
opacity: 1;
}
.fade-exit-active {
opacity: 0;
transition: opacity 200ms ease-out;
}
isShowing
state variable:const [isShowing, setIsShowing] = useState(false);
const handleClick = () => {
setIsShowing(!isShowing);
};
<button onClick={handleClick}>Toggle Animation</button>
By following these steps, you can add animations to your React components using the react-transition-group library. You can explore other features of the library such as <TransitionGroup>
for managing multiple elements with animations.