Jump to content
GreenSock

newusergsap

React with GSAP causing multiple trigger on scroll and showing multiple marker

Go to solution Solved by GSAP Helper,

Recommended Posts

I have a single page website where each section is a component. Every section has some animation triggered on the scroll event. Every animation in every section gets trigged twice on scroll. To debug this, I have just enabled markers one by one in every component to see what's the problem. Surprisingly what I have found that every section has repeated markers  - 2 starts and 2 ends. When first "start marker" hits the viewport center, it trigger the animation and when again the second "start marker" hits the viewport center, it trigger the animation again. 

 

The code is simple and there is nothing I am not doing anything fancy. As the code suggest animation brings the list item in one by one - 

 

 useEffect(()=>{
        gsap.defaults({ ease: "power3" });  
   
        const blogListArray = gsap.utils.toArray(".blog-list-item");
        blogListArray.forEach((item, index)=> { 
            gsap.fromTo(item, {xPercent:-50*index,opacity:0},{xPercent:0,opacity:1, duration:0.5, scrollTrigger:{
                    trigger: "#section-list", 
                    toggleActions: "play none none reverse", 
                    start: 'center center', // trigger element, viewport  
                    end:"bottom center",
                    markers:true 
            }}) 
        })
         
    },[])

 

 

Link to comment
Share on other sites

  • Solution

Perhaps 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?

 

In GSAP 3.11, we introduced a new gsap.context() feature that solves all of this for you. 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!
}, []);

 

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

 

We strongly recommend reading the React article at https://greensock.com/react

 

Happy tweening!

  • Like 2
Link to comment
Share on other sites

Thank you this explains everything. Thank you.

  • Like 1
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.
×