|
| 1 | +--- |
| 2 | +title: GSAP |
| 3 | +seoTitle: Using the newly added useGSAP() hook as a drop-in replacement for UseEffect() |
| 4 | +summary: GSAP just added useGSAP() hook for React |
| 5 | +isReleased: true |
| 6 | +isSequel: false |
| 7 | +lastModDate: 2024-01-05T09:15:00-0401 |
| 8 | +firstModDate: 2024-01-05T09:15:00-0401 |
| 9 | +minutesToRead: 2 |
| 10 | +tags: |
| 11 | + - 'react' |
| 12 | + - 'gsap' |
| 13 | +--- |
| 14 | + |
| 15 | +<H2>New Hook Just Dropped</H2> |
| 16 | +<C> |
| 17 | +Over the past few years, I've primarily relied on <L href="/blog/framer-motion">Framer Motion</L> for handling my animations, while incorporating <L href="https://gsap.com/">GSAP</L> from time to time, I found it to be somewhat verbose compared to **Framer**, especially in a **React** context. |
| 18 | + |
| 19 | +But, check this out, yesterday **GSAP** introduced the **``useGSAP()``** hook, an addition to easily integrate **GSAP** with **React**. |
| 20 | +<S/> |
| 21 | +Prior to this, animating elements involved wrapping them in a **``gsap.Context``** object, then, you had to use the same object's methods like **``.revert()``** or **``.kill()``** within **``useEffect()``** or **``useLayoutEffect()``**'s cleanup function to avoid memory leaks, depending if you're within an **SSR** context or not, along with a mandatory dependency array that you have to keep track of. It's just a lot of overhead really. |
| 22 | + <S3/> |
| 23 | + To do something silly like |
| 24 | +</C> |
| 25 | +<TTLMO/> |
| 26 | +<C>You'd probably need to</C> |
| 27 | +<Code |
| 28 | + code={`import React, { useRef, useEffect, useLayoutEffect } from 'react'; |
| 29 | +import gsap from 'gsap'; |
| 30 | +
|
| 31 | +const useIsoLayoutEffect = |
| 32 | + typeof window !== 'undefined' ? useLayoutEffect : useEffect; |
| 33 | +
|
| 34 | +const Component: React.FC = () => { |
| 35 | + const container = useRef<HTMLDivElement>(null); |
| 36 | +
|
| 37 | + useIsoLayoutEffect(() => { |
| 38 | + const ctx = gsap.context(() => { |
| 39 | + const tl = gsap.timeline({ repeat: -1, repeatDelay: 0.5 }); |
| 40 | +
|
| 41 | + tl.to('.x', { |
| 42 | + rotation: 360, |
| 43 | + duration: 2, |
| 44 | + borderRadius: 16, |
| 45 | + translateX: -150, |
| 46 | + ease: 'power1.inOut', |
| 47 | + }); |
| 48 | + tl.to('.x', { |
| 49 | + rotation: -360, |
| 50 | + duration: 2, |
| 51 | + borderRadius: 0, |
| 52 | + translateX: 150, |
| 53 | + ease: 'power1.inOut', |
| 54 | + }); |
| 55 | + tl.to('.x', { |
| 56 | + rotation: 360, |
| 57 | + duration: 2, |
| 58 | + borderRadius: 16, |
| 59 | + translateX: 0, |
| 60 | + ease: 'power1.inOut', |
| 61 | + }); |
| 62 | + }, container); |
| 63 | +
|
| 64 | + return () => ctx.revert(); |
| 65 | + }, []); |
| 66 | +
|
| 67 | + return ( |
| 68 | + <div> |
| 69 | + <div |
| 70 | + ref={container} |
| 71 | + className="flex flex-col items-center justify-center gap-10" |
| 72 | + > |
| 73 | + <div className="x w-10 h-10 bg-red-400 rounded-2xl animate-pulse"></div> |
| 74 | + <div className="x w-10 h-10 bg-yellow-400 rounded-2xl animate-pulse"></div> |
| 75 | + <div className="x w-10 h-10 bg-green-500 rounded-2xl animate-pulse"></div> |
| 76 | + </div> |
| 77 | + </div> |
| 78 | + ); |
| 79 | +}; |
| 80 | +`} |
| 81 | + language="tsx" |
| 82 | + showLineNumbers={false} |
| 83 | +/> |
| 84 | +<C>This new hook abstracts away these inconveniences, and obviates the need for explicit cleanup. |
| 85 | + So the same logic above can be written as </C> |
| 86 | +<Code |
| 87 | + code={` |
| 88 | + useGSAP( |
| 89 | + () => { |
| 90 | + const tl = gsap.timeline({ repeat: -1, repeatDelay: 0.5 }); |
| 91 | + tl.to('.x', { |
| 92 | + rotation: 360, |
| 93 | + duration: 2, |
| 94 | + borderRadius: 16, |
| 95 | + translateX: -150, |
| 96 | + ease: 'power1.inOut', |
| 97 | + }); |
| 98 | + tl.to('.x', { |
| 99 | + rotation: -360, |
| 100 | + duration: 2, |
| 101 | + borderRadius: 0, |
| 102 | + translateX: 150, |
| 103 | + ease: 'power1.inOut', |
| 104 | + }); |
| 105 | + tl.to('.x', { |
| 106 | + rotation: 360, |
| 107 | + duration: 2, |
| 108 | + borderRadius: 16, |
| 109 | + translateX: 0, |
| 110 | + ease: 'power1.inOut', |
| 111 | + }); |
| 112 | + }, |
| 113 | + { scope: container} |
| 114 | + ); |
| 115 | +`} |
| 116 | + language="tsx" |
| 117 | + showLineNumbers={false} |
| 118 | +/> |
| 119 | +<C>It looks hella cleaner, I actually like this update, I mean it's not a lot but hey, it gets the job done |
| 120 | +a lot easier so, I might use even more now, and sorry if you thought this is an educational blog where I teach you about **GSAP**, nope, I'm just glad they introduced this new hook, that's it.</C> |
0 commit comments