
using-gsap-with-react
by ban12-project
SKILL.md
name: Using GSAP with React description: Best practices for integrating GSAP animations in React applications using the @gsap/react hook.
GSAP with React Best Practices
This skill provides guidelines and patterns for using GSAP (GreenSock Animation Platform) effectively within React applications, specifically leveraging the official @gsap/react hook.
1. Installation and Setup
Always use the official @gsap/react package which simplifies cleanup and integration.
npm install gsap @gsap/react
Registration
Register the useGSAP hook as a plugin to avoid discrepancies in React versions and ensure it's ready for use.
import gsap from 'gsap';
import { useGSAP } from '@gsap/react';
gsap.registerPlugin(useGSAP);
2. Core Hook: useGSAP()
The useGSAP() hook is a drop-in replacement for useEffect or useLayoutEffect. It automatically handles cleanup by reverting all animations created during its execution.
Basic Usage with Scope
Using a scope (a ref to a container element) is the recommended way to use selector text (e.g., ".box") safely within GSAP.
import { useRef } from 'react';
import gsap from 'gsap';
import { useGSAP } from '@gsap/react';
const MyComponent = () => {
const container = useRef<HTMLDivElement>(null);
useGSAP(() => {
// Selectors are scoped to 'container'
gsap.to('.box', { x: 100, stagger: 0.1 });
}, { scope: container });
return (
<div ref={container}>
<div className="box">Box 1</div>
<div className="box">Box 2</div>
</div>
);
};
Advanced Config Object
For more control, pass a config object as the second argument.
useGSAP(() => {
gsap.to('.box', { x: endX });
}, {
dependencies: [endX],
scope: container,
revertOnUpdate: true // Reverts and re-runs when endX changes
});
dependencies: Similar touseEffectdependency array. Defaults to[].scope: Limits selector text to descendants of the provided React Ref.revertOnUpdate: Defaults tofalse. Iftrue, the hook will revert and clean up everything when a dependency changes. Iffalse, it only reverts on unmount.
3. Handling Interactions & Event Handlers
Animations created inside event handlers (like onClick) are NOT automatically cleaned up because they occur after the useGSAP hook has executed. Use contextSafe to make them "safe".
const container = useRef<HTMLDivElement>(null);
const { contextSafe } = useGSAP({ scope: container });
const onClick = contextSafe(() => {
// This animation will be reverted on unmount because it's 'contextSafe'
// and selector text is scoped to 'container'
gsap.to('.box', { rotation: '+=360' });
});
return (
<div ref={container}>
<div className="box">Animate Me</div>
<button onClick={onClick}>Rotate</button>
</div>
);
4. ScrollTrigger in React
When using ScrollTrigger, ensure you create it inside useGSAP. It will be automatically cleaned up.
useGSAP(() => {
gsap.to('.box', {
scrollTrigger: {
trigger: '.box',
start: 'top center',
scrub: true
},
x: 500
});
}, { scope: container });
5. Controlling Animations: Refs vs State
- Storing Timelines: Use
useRefto store timelines or tweens that you need to control (play/pause/reverse) later. - Reacting to State: Pass dependencies to the
useGSAPconfig object.
const tl = useRef<gsap.core.Timeline>();
const [reversed, setReversed] = useState(false);
useGSAP(() => {
tl.current = gsap.timeline().to('.box', { x: 100 });
}, { scope: container });
// Update on state change
useGSAP(() => {
tl.current?.reversed(reversed);
}, [reversed]);
6. Component Communication
Passing Timelines as Props
If a parent needs to orchestrate child animations, pass a timeline via useState (not useRef) to ensure the child can access it during its first render.
// Parent
const [tl, setTl] = useState<gsap.core.Timeline>();
useGSAP(() => {
setTl(gsap.timeline());
});
return <Child timeline={tl} />;
// Child
useGSAP(() => {
timeline?.to(el.current, { opacity: 1 });
}, [timeline]);
7. Exit Animations
To animate an element as it leaves the DOM, use a state variable to control rendering and onComplete to update that state. Combine with contextSafe for reliability.
const { contextSafe } = useGSAP({ scope: container });
const [isVisible, setIsVisible] = useState(true);
const remove = contextSafe(() => {
gsap.to('.box', {
opacity: 0,
onComplete: () => setIsVisible(false)
});
});
return (
<div ref={container}>
{isVisible && <div className="box">Goodbye</div>}
<button onClick={remove}>Exit</button>
</div>
);
8. Next.js & Server-Side Rendering (SSR)
- Always use
"use client";at the top of files usinguseGSAP. useGSAPhandles "Isomorphic Layout Effect" internally (it usesuseLayoutEffecton the client anduseEffecton the server if needed).
9. Performance & Hygiene
- Revert is King:
useGSAPautomatically callsctx.revert(). Do not manually cleanup unless you have a very specific edge case. - Avoid
useEffect: Always preferuseGSAPoveruseEffectoruseLayoutEffectfor animation logic. - Scoped Selectors: Use them to keep your code clean and avoid targeting elements outside the component.
- Register Plugins: Ensure plugins like
ScrollTrigger,Flip, orDraggableare registered withgsap.registerPlugin(...).
10. Common Pitfalls
- Creating Tweens Outside Hook: Never create
gsap.to()at the top level of a component or outside ofuseGSAP/contextSafe. - Missing Dependencies: If your animation depends on a state/prop, include it in the dependency array of
useGSAPto avoid stale closures. - Ref Mismanagement: Always check
ref.currentbefore using it if you aren't using thescopeparameter. - Strict Mode: React 18 Strict Mode runs effects twice in dev.
useGSAPhandles this correctly by reverting the first set of animations, but only if you use it correctly!
スコア
総合スコア
リポジトリの品質指標に基づく評価
SKILL.mdファイルが含まれている
ライセンスが設定されている
100文字以上の説明がある
GitHub Stars 100以上
3ヶ月以内に更新がある
10回以上フォークされている
オープンIssueが50未満
プログラミング言語が設定されている
1つ以上のタグが設定されている
レビュー
レビュー機能は近日公開予定です