Jump to content
Search Community

Recalculate ScrollSmoother on React Route Change

fakesamgregory test
Moderator Tag

Recommended Posts

I have a React project where I'm using ScrollSmoother and where the route changes in height, the ScrollSmoother does not recalculate. I can force it by resizing the window. 

 

I have tried smoother.scrollTrigger.refresh(), ScrollTrigger.update() to no avail...

 

I'm happy to share my project but there's a few moving parts but wanted to first know if I'm misunderstanding something simple or somebody knows how I might go about this.

 

I guess where the magic happens might be a simple way to start. I'm doing it in the useEffect hook and am able to trigger on route change


 

useEffect(() => {    
    smoother = ScrollSmoother.create({
      effects: true,
      smoothTouch: 0.1,
    }); 
    
    ScrollTrigger.refresh()
  }, [location])

 

Link to comment
Share on other sites

Just now, fakesamgregory said:

Just wondering if this was an issue that's been encountered before I create a project.

 

It's a new plugin, so there have been some bugs reported, but it's hard to tell if you're doing is one of those bugs without seeing the issue.

 

2 minutes ago, fakesamgregory said:

I can't right now but I can tomorrow.

 

Thanks. And of course the sooner the better as we want to push out the next patch pretty sooon.

 

Link to comment
Share on other sites

@OSUblake I managed to create a sandbox environment and it worked just fine.

 

My specific use case was the loading of images to which I solved by looping through the images and calling ScrollTrigger.refresh().

 

Maybe this'll help others.

 

useEffect(() => {
  if (!smoother) {
    setSmoother(ScrollSmoother.create({
      effects: true,
      smoothTouch: 0.1,
    }))
  }

  // refresh once all images load
  Array.from(document.querySelectorAll('img')).forEach(img => {
    img.addEventListener('load', function() {
      ScrollTrigger.refresh();
    })
  })

  return () => {
    smoother?.kill();
  };
}, [location]);
 
  • 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.
×
×
  • Create New...