Share Posted December 31, 2022 When the page is reloaded, the animation is not performed. What is the problem? const array = [ { title: "Ref Element 1" }, { title: "Ref Element 2" }, { title: "Ref Element 3" } ]; export function Slider() { const titleH1Refs = useRef([]); titleH1Refs.current = []; //checking for an existing element in an array const addToRefsTitleH1 = el => { if (el && !titleH1Refs.current.includes(el)) { titleH1Refs.current.push(el); } }; //perform animation for all array elements useLayoutEffect(() => { titleH1Refs.current.forEach((element) => { gsap.from(element, { opacity: 0, y: 20, ease: Expo.easeInOut }) }) }, [titleH1Refs.current]); return ( <section className="container"> {array.map((element) => ( <div className="element" ref={addToRefs}>{element.title}</div> ))} </section> ); } Link to comment Share on other sites More sharing options...
Solution Solution Share Posted December 31, 2022 Welcome to the forums, @AlexanderGS The problem is that React 18 calls your useEffect() TWICE in strict mode! Very annoying. It has caused a lot of headaches for a lot of people outside the GSAP community too. .from() tweens use the CURRENT value as the destination and it renders immediately the value you set in the tween, so when it's called the first time it'd work great but if you call it twice, it ends up animating from the from value (no animation). It's not a GSAP bug - it's a logic thing. For example, let's say el.x is 0 and you do this: useEffect(() => { // what happens if this gets called twice? gsap.from(el, {x: 100}) }, []); The first time makes el.x jump immediately to 100 and start animating backwards toward the current value which is 0 (so 100 --> 0). But the second time, it would jump to 100 (same) and animate back to the current value which is now 100 (100 --> 100)! See the issue? In GSAP 3.11, we introduced a new gsap.context() feature that solves all of this for you. It makes cleanup super easy. All you need to do is wrap your code in a context call, and then return a cleanup function that reverts things: // typically it's best to useLayoutEffect() instead of useEffect() to have React render the initial state properly from the very start. useLayoutEffect(() => { let ctx = gsap.context(() => { // all your GSAP animation code here }); return () => ctx.revert(); // <- cleanup! }, []); I'd strongly recommend reading the React article at So in your demo... // OLD useLayoutEffect(() => { titleH1Refs.current.forEach((element) => { gsap.from(element, { opacity: 0, y: 20, ease: Expo.easeInOut }) }) }, [titleH1Refs.current]); // NEW useLayoutEffect(() => { let ctx = gsap.context(() => { gsap.from(titleH1Refs.current, { opacity: 0, y: 20, ease: "expo.inOut" }); }); return () => ctx.revert(); // <-- cleanup! }, [titleH1Refs.current]); You don't need to loop through each element and create a separate tween for each one. You can feed them all into one. It's totally fine if you prefer that loop - it's just not necessary. You might also want to look into the selector scoping in gsap.context() because it can save you from the hassle of creating/managing a bunch of Refs. Enjoy! 2 1 Link to comment Share on other sites More sharing options...
Author Share Posted January 1 Good afternoon! @GreenSock I read the documentation and was able to solve the problem. Thank you for your help😄 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now