Jump to content
Search Community

Scroll Trigger breaking using React Router

ReactiveGuy test
Moderator Tag

Go to solution Solved by Rodrigo,

Recommended Posts

I'm building a landing page using React Router that has a scrolling menu. Based on the position of the scrolling menu, the background elements change. Everything works, until I navigate to a different page, and receive Uncaught TypeError: Cannot read property 'classList' of null. When I return to the landing page, the ScrollTrigger is broken until the site is refreshed. I know this is due to the component unmounting and ScrollTrigger not being able to find the elements triggering onLeave and onLeaveBack. I tried using useLocation() to make sure ScrollTrigger instantiate when the the pathname = "/" and otherwise kill() the trigger, but it didn't solve the issue.

 

So the question is, how do I navigate away from my landing page without having my ScrollTrigger break and require a full refresh?

 

Here is the structure of my ScrollTrigger:

const pages = ["radio", "shop", "music", "about", "news"];
    useEffect(() => {
        const backgroundSquare = document.querySelector(".home-menu-background");
        pages.forEach((page) => {
            gsap.to(`#home-menu-item-${page}`, {
                scrollTrigger: {
                    trigger: `#home-menu-item-${page}`,
                    start: "top center",
                    end: "bottom center",
                    onEnter: () => {
                        backgroundSquare.style.backgroundImage = `url("${backgroundImages[page]}")`;
                        document.querySelector(`#home-menu-item-${page}`).classList.add("active");
                    },
                    onEnterBack: () => {
                        backgroundSquare.style.backgroundImage = `url("${backgroundImages[page]}")`;
                        document.querySelector(`#home-menu-item-${page}`).classList.add("active");
                    },
                    onLeave: () => {
                        document.querySelector(`#home-menu-item-${page}`).classList.remove("active");
                    },
                    onLeaveBack: () => {
                        document.querySelector(`#home-menu-item-${page}`).classList.remove("active");
                    },
                },
            });
        });
    });

Link to comment
Share on other sites

  • Solution

Hi,

 

The useEffect() hook can return a function that is executed when the component is unmounted (something React Router does for you), in that function you have to kill your ScrollTrigger instances in order to prevent that type of issues, something like this:

useEffect(() => {
  // All your code here
  return () => {
    // Cleanup here, kill your ScrollTrigger instances here
  };
}, []);

I don't have a live sample to show right now. I'll try to make something simple during the weekend. Hopefully this will be enough to get you started right now.

 

Happy Tweening!!!

  • Like 2
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...