Jump to content
Search Community

fuhuahu

Members
  • Posts

    1
  • Joined

  • Last visited

Posts posted by fuhuahu

  1. On 6/17/2022 at 2:03 AM, GSAP Helper said:

    One more thing...I noticed you're using a "from()" tween. You may be running into the React 18 issue that runs in "strict" mode locally by default which causes your useEffect() to get called TWICE! 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?

     

    So you can either turn off strict mode in React or you can add some conditional logic to your useEffect() call so that it only runs ONCE. Sorta like:

    const didAnimate = useRef(false);
    
    useEffect(() => {
      // if we already ran this once, skip!
      if (didAnimate.current) { return; }
      // otherwise, record that we're running it now and continue...
      didAnimate.current = true;
      gsap.from(el, {x: 100});
    }, []);

     

    Or you could just use .fromTo() tweens so that you define both the start and end values.

     

    One of the React team members chimed in here if you'd like more background.

    Nothing can express my appreciation to you, this same issue has tortured me all night long, you are my hero!

    • Like 1
×
×
  • Create New...