Last active
February 20, 2023 15:19
-
-
Save mattgperry/e526065c42d503b11b28a43349526dcd to your computer and use it in GitHub Desktop.
Simplify the use of Greensock and React
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function useGreensock(callback, deps, optionalScope) { | |
const defaultScope = useRef() | |
const scope = optionalScope || defaultScope | |
useLayoutEffect(() => { | |
const context = gsap.context(callback, scope) | |
return () => context.revert() | |
}, deps) | |
return scope | |
} | |
/** | |
function Component({ isVisible }) { | |
const scope = useGreensock(container, () => { | |
gsap.to(".box", { opacity: isVisible ? 1 : 0 }) | |
}, [isVisible]) | |
return ( | |
<div ref={scope}> | |
<div className="box" /> | |
</div> | |
) | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment