Skip to content

Instantly share code, notes, and snippets.

@mattgperry
Last active February 20, 2023 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattgperry/e526065c42d503b11b28a43349526dcd to your computer and use it in GitHub Desktop.
Save mattgperry/e526065c42d503b11b28a43349526dcd to your computer and use it in GitHub Desktop.
Simplify the use of Greensock and React
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