Jump to content
Search Community

gsap pinning elements in next js and locomotive scroll (beta)

Nafiz Ahmed test
Moderator Tag

Go to solution Solved by Rodrigo,

Recommended Posts

Posted (edited)

I'm trying to implement a GSAP timeline with Locomotive Scroll beta  to pin an element while scrolling in a Next.js component. The code works perfectly in a normal HTML and JS environment, but it's not functioning as expected in Next.js.

Here is the minimal demo with normal html environment:

Here is the Next Js version:


I'm new to GSAP and I'm looking for assistance to fix the issue.

Edited by Nafiz Ahmed
specifying the version
Link to comment
Share on other sites

  • Nafiz Ahmed changed the title to gsap pinning elements in next js and locomotive scroll (beta)
  • Solution

Hi @Nafiz Ahmed and welcome to the GSAP Forums!


Unfortunately your NextJS demo is not accessible (I'm getting a 404) so there is not a lot we can do tot helps, I'm afraid.


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. 

  • Thanks 1
Link to comment
Share on other sites



I apologize for the 404 error with my NextJS demo. It turned out to be a permission issue that I've since corrected (https://codesandbox.io/p/devbox/c4cq6q?file=%2Fpin-element%2Fsrc%2Fapp%2FPinComponent.js%3A28%2C11).


However, I'm happy to report that the useGSAP hook has been a lifesaver! It completely resolved the issues I was having with animation cleanup in React, especially with strict mode enabled.


A huge thank you to @Rodrigo for the fantastic support!

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