Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gsap with scrub not working on react on first mount (?) #602

Open
bandinopla opened this issue Nov 5, 2024 · 3 comments
Open

gsap with scrub not working on react on first mount (?) #602

bandinopla opened this issue Nov 5, 2024 · 3 comments

Comments

@bandinopla
Copy link

I'm using react (18.3.1)
Why could it be that this doesn't work at the start if I refresh the page that has this component, but either if I open the dev console (F12) it starts working or if I navigate to another link on my site and then come back... it is weird.

    useGSAP(
        () => {  
          
                const boxes = gsap.utils.toArray('.featureBox');
                      boxes.forEach((box) => {
                          gsap.from(box, {
                                          x: 250,
                                          scrollTrigger: {
                                              trigger: box,
                                              start: 'top bottom',
                                              end: 'top 10%',
                                              scrub: true,  
                                              markers:true 
                                          }
                          });
                      });  
         
        },
        { scope: featuresRef }
      ); 
@bandinopla bandinopla changed the title gsap with scrut not working on react at start (?) gsap with scrub not working on react at start (?) Nov 5, 2024
@bandinopla bandinopla changed the title gsap with scrub not working on react at start (?) gsap with scrub not working on react on first mount (?) Nov 5, 2024
@jackdoyle
Copy link
Member

It's super difficult to troubleshoot without a minimal demo - can you provide a stackblitz that clearly illustrates the issue? Here's a starter template you can fork: https://stackblitz.com/edit/react-cxv92j

It sounds like maybe some kind of issue with your build process (maybe). Did you check to see if the code is actually running when you expect? And are there .featureBox elements in the DOM at that point?

@bandinopla
Copy link
Author

@jackdoyle there's nothing special about my code, I just basically copy pasted the code from here: https://stackblitz.com/edit/react-cxv92j?file=src%2FApp.js

The weird thing is that it works ok if I just open the dev console, or if i navigate to another page and come back to the page that has the element...

Messing arround if I wrap the code inside the useGSAP in a requestAnimationFrame it also works (but it breaks the internal cleanup on unmount so not gonna use it)

sometimes changing from gsap.fromto gsap.to also sometimes make it work... it is weird. It is hard to reproduce.
Mabe it has something to do with devmode in react 18 ? the double useEffect trigger??

@jackdoyle
Copy link
Member

Are you able to reproduce the problem in that Stackblitz link you shared? I can't.

I don't think the problem is in your code - it kinda sounds like a build process issue on your end maybe. The double-invocation thing in React 18+ is solved by useGSAP() and its cleanup.

I just need a way to clearly reproduce the problem you're seeing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants