Mastering GSAP Animations in React
Learn how to create smooth, performant animations in your React applications using GSAP and ScrollTrigger.
GSAP (GreenSock Animation Platform) is the gold standard for web animations. Here's how to integrate it effectively with React.
Setting Up GSAP
First, install GSAP and its React plugin:
npm install gsap @gsap/reactUsing gsap.context()
Always scope your animations with gsap.context() for proper cleanup:
useEffect(() => {
const ctx = gsap.context(() => {
gsap.from(element, { opacity: 0, y: 20 })
})
return () => ctx.revert()
}, [])ScrollTrigger Animations
ScrollTrigger lets you trigger animations based on scroll position.
Performance Tips
- Use
will-change: transformon animated elements - Respect
prefers-reduced-motion - Scope animations properly