You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+9-2Lines changed: 9 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -30,6 +30,8 @@ import { useRef } from "react";
30
30
importgsapfrom"gsap";
31
31
import { useGSAP } from"@gsap/react";
32
32
33
+
gsap.registerPlugin(useGSAP); // register any plugins, including the useGSAP hook
34
+
33
35
constcontainer=useRef();
34
36
useGSAP(() => {
35
37
// gsap code here...
@@ -84,9 +86,14 @@ useGSAP(() => {
84
86
npm install @gsap/react
85
87
```
86
88
89
+
At the top of your code right below your imports, it's usually a good idea to register `useGSAP` as a plugin:
90
+
```javascript
91
+
gsap.registerPlugin(useGSAP);
92
+
```
93
+
87
94
## Using callbacks or event listeners? Use `contextSafe()` and clean up!
88
95
89
-
A function is considered "context-safe" if it is properly scoped to a <ahref="https://gsap.com/docs/v3/GSAP/gsap.context()">`gsap.context()`</a> so that any GSAP-related objects created **while that function executes** are recorded by that `Context` and use its `scope` for selector text. When that `Context` gets reverted (like when the hook gets torn down or re-synchronizes), so do all of those GSAP-related objects. Cleanup is important in React and `Context` makes it simple. Otherwise, you'd need to manually keep track of all your animations and `revert()` them when necessary, like when the entire component gets unmounted/remounted. `Context` does that work for you.
96
+
A function is considered "context-safe" if it is properly scoped to a <ahref="https://gsap.com/docs/v3/GSAP/gsap.context()">`gsap.context()`</a> so that any GSAP-related objects created **while that function executes** are recorded by that `Context` and use its `scope` for selector text. When that `Context` gets reverted (like when the hook gets torn down or re-synchronizes), so will all of those GSAP-related objects. Cleanup is important in React and `Context` makes it simple. Otherwise, you'd need to manually keep track of all your animations and `revert()` them when necessary, like when the entire component gets unmounted/remounted. `Context` does that work for you.
90
97
91
98
The main `useGSAP(() => {...})` function is automatically context-safe of course. But if you're creating functions that get called **AFTER** the main `useGSAP()` function executes (like click event handlers, something in a `setTimeout()`, or anything delayed), you need a way to make those functions context-safe. Think of it like telling the `Context` when to hit the "record" button for any GSAP-related objects.
92
99
@@ -208,4 +215,4 @@ Ask in the friendly <a href="https://gsap.com/community/">GSAP forums</a>. Or sh
208
215
### License
209
216
GreenSock's standard "no charge" license can be viewed at <ahref="https://gsap.com/standard-license">https://gsap.com/standard-license</a>. <ahref="https://gsap.com/pricing/">Club GSAP</a> members are granted additional rights. See <ahref="https://gsap.com/licensing/">https://gsap.com/licensing/</a> for details. Why doesn't GSAP use an MIT (or similar) open source license, and why is that a **good** thing? This article explains it all: <ahref="https://gsap.com/why-license/"target="_blank">https://gsap.com/why-license/</a>
210
217
211
-
Copyright (c) 2008-2024, GreenSock. All rights reserved.
218
+
Copyright (c) 2008-2025, GreenSock. All rights reserved.
if(!deferCleanup||!mounted.current){// React renders bottom-up, thus there could be hooks with dependencies that run BEFORE the component mounts, thus cleanup wouldn't occur since a hook with an empty dependency Array would only run once the component mounts.
0 commit comments