Jump to content


Code works in production, not locally?

Recommended Posts

   Hello,  I truly enjoy using GSAP.  I have one issue and i'm not sure I can duplicate it, I do know you require minimum code example for best solution. My code works in production and not locally. All code is same, when I launch it locally the animations freeze, yet when deployed it works fine. I am using react with "ScrollTrigger". Any suggestions you can think of if not I will have to figure out how to duplicate the issue.

Link to comment
Share on other sites

Let me guess - you're using React?


If so, I bet the problem is that React 18 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.


Stay tuned for the next release of GSAP because we've got some new features that'll make it even easier to work around React 18's odd behavior. 

Link to comment
Share on other sites

Thank you!! This is the reason it works in production and not locally?  Production code works fine! Also do I have to use it in every useEffect? 

Link to comment
Share on other sites

I'm not a React guy but my understanding is that by default, React 18 runs in "Strict Mode" locally (but NOT production). The useEffect() double-call is only done by React in Strict Mode. So yes. 


I assume it is in every useEffect(), yes. Repeat disclaimer: I'm definitely not a React guy. I try to avoid frameworks as much as possible :)

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.