Jump to content
Search Community

React customHook issues. Events firing on render in ScrollTrigger

GregQP test
Moderator Tag

Recommended Posts

Each time a page changes in the app a new scrolltrigger is created and its trigger is a div that I already instantiated.

 

I'm finding that during these re-renders the scroll-trigger callbacks are firing with self.progress = 1. As a result I'm only firing callbacks when self.progress is between  0.1 and 0.9. This works ok but means  tweens don't finish before the callback fires and the instance is destroyed. So it's jerky.


What's the best solution to not having callbacks fire when scrolltrigger is going to the create/destroy cycle that React likes to do?

 

Here's a stackblitz that shows what I'm encountering. Open the console, scroll the app to trigger an onLeave event and they keep on firing as new scrolltriggers are created.

 

https://stackblitz.com/edit/react-gz37v5?file=src%2FApp.jsx

 

Link to comment
Share on other sites

  • GregQP changed the title to React customHook issues. Events firing on render in ScrollTrigger

Without a minimal demo, it's very difficult to troubleshoot; the issue could be caused by CSS, markup, a third party library, a 3rd party script, etc. Would you please provide a very simple CodePen or Stackblitz that illustrates the issue? 

 

Please don't include your whole project. Just some colored <div> elements and the GSAP code is best. See if you can recreate the issue with as few dependencies as possible. Start minimal and then incrementally add code bit by bit until it breaks. Usually people solve their own issues during this process! If not, at least we have a reduced test case which greatly increases your chances of getting a relevant answer.

 

See the Pen aYYOdN by GreenSock (@GreenSock) on CodePen

that loads all the plugins. Just click "fork" at the bottom right and make your minimal demo

 

Using a framework/library like React, Vue, Next, etc.? 

CodePen isn't always ideal for these tools, so here are some Stackblitz starter templates that you can fork and import the gsap-trial NPM package for using any of the bonus plugins: 

 

Please share the StackBlitz link directly to the file in question (where you've put the GSAP code) so we don't need to hunt through all the files. 

 

Once we see an isolated demo, we'll do our best to jump in and help with your GSAP-specific questions. 

Link to comment
Share on other sites

Hi there! I see you're using React -

Proper cleanup is very important with frameworks, but especially with React. React 18 runs in strict mode locally by default which causes your useEffect() and useLayoutEffect() to get called TWICE.

 

Since GSAP 3.12, we have the useGSAP() hook (the NPM package is here) that simplifies creating and cleaning up animations in React (including Next, Remix, etc). It's a drop-in replacement for useEffect()/useLayoutEffect(). All the GSAP-related objects (animations, ScrollTriggers, etc.) created while the function executes get collected and then reverted when the hook gets torn down.

 

Here is how it works:

const container = useRef(); // the root level element of your component (for scoping selector text which is optional)

useGSAP(() => {
  // gsap code here...
}, { dependencies: [endX], scope: container }); // config object offers maximum flexibility

Or if you prefer, you can use the same method signature as useEffect():

useGSAP(() => {
  // gsap code here...
}, [endX]); // simple dependency Array setup like useEffect()

This pattern follows React's best practices.

 

We strongly recommend reading the React guide we've put together at https://gsap.com/resources/React/

 

If you still need help, here's a React starter template that you can fork to create a minimal demo illustrating whatever issue you're running into. Post a link to your fork back here and we'd be happy to take a peek and answer any GSAP-related questions you have. Just use simple colored <div> elements in your demo; no need to recreate your whole project with client artwork, etc. The simpler the better. 

Link to comment
Share on other sites

Hi,

 

I fail to see the logic behind what you're doing in this demo TBH. Why you need that custom hook? What is actually doing? Why create and use that hook instead of the useGSAP hook?

 

Concerning your initial question: What exactly is jerky in your demo? You are creating some GSAP Timelines with your hook but those are not animating anything, they just have a ScrollTrigger configuration, but nothing more, so there is nothing animating so there  is nothing behaving jerky in your demo. Maybe I'm missing something obvious here 🤷‍♂️

 

Happy Tweening!

Link to comment
Share on other sites

If you look at the StackBlitz I created you can see that instances of ScrollTrigger are firing their onleave event on instantiation. But as they haven’t been scrolled they shouldn’t fire an onleave event 

 

A react hook abstracts logic to make it reusable. ueaGSAP is the same. 
 

The hook I created does the same except it also contains tweens and attaches the timeline for those tweens to a ScrollTrigger that then uses a master div as its scroller.  It’s how I designed it based on other answers in this forum.
 

With the hook I can just pass a ref in from amy component and have it animate.  If I just used useGSAP I’d write a lot more code. Which is the cornerstone of react.. reusable functions that save you repeating yourself yourself 

Link to comment
Share on other sites

11 hours ago, GregQP said:

If you look at the StackBlitz I created you can see that instances of ScrollTrigger are firing their onleave event on instantiation. But as they haven’t been scrolled they shouldn’t fire an onleave event 

That's because the first  ScrollTrigger instance is passed the end marker and the second one is passed the start marker, makes total sense:

slsCFTH.pngYou have to take a look at the clamp feature in ScrollTrigger:

 

11 hours ago, GregQP said:

A react hook abstracts logic to make it reusable. ueaGSAP is the same. 
 

The hook I created does the same except it also contains tweens and attaches the timeline for those tweens to a ScrollTrigger that then uses a master div as its scroller.  It’s how I designed it based on other answers in this forum.
 

With the hook I can just pass a ref in from amy component and have it animate.  If I just used useGSAP I’d write a lot more code. Which is the cornerstone of react.. reusable functions that save you repeating yourself yourself 

Yep I'm aware of what custom hooks do in React, I wrote parts of our useGSAP hook.

 

Hopefully this helps.

Happy Tweening!

Link to comment
Share on other sites

The name onLeave implies the trigger is leaving the trigger zone not that it is outside. If I instantiated five scroll triggers four of which were outside the viewport

you’re saying four onLeave events firing makes sense?   If the code is firing events that should be triggered by user interaction on instantiation then that’s a bug. If that’s not the case then  the name of the onleave callback should be something that reflects its purpose accurately. 
 

I’ll look at clamp. Thanks for the tip. 

Link to comment
Share on other sites

Hi,

 

The issue is in this funky useEffect hook  you have here:
 

useEffect(() => {
  setInterval(() => {
    setState(Math.random());
  }, 1500);
});

That basically runs everytime the component is re-rendered since it has no dependencies array, but at the same time each time the interval is completed the state is updated, creating a new interval and running your custom hook again, so after the first interval is completed a new one is created and each new interval will create a new one, so this grows exponentially.

 

Here is a fork of your demo without that and it has only two onLeave callbacks, which makes sense since the effect hooks are called twice on StrictMode:

https://stackblitz.com/edit/react-pps835?file=src%2FApp.jsx

 

Hopefully this clear things up.

Happy Tweening!

Link to comment
Share on other sites

Hi. 
 

I would appreciate it if you could take the time to understand my question fully.

 

The timeout is intentional as it demonstrates that when a ScrollTriggers is instantiated due to the state change of a parent component the newly instantiated ScrollTriggers fire on leave events without any user interaction. In order to see this go to the stack blitz demo, open the console, scroll and the see the onleave events fire without any interaction. 
 

it doesn’t grow exponentially. The ScrollTriggers are unmounted and rectreated but it always only 5. 


 

I would like to know why the onleave events fire without any user interaction. 

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.
×
×
  • Create New...