SauravKS Posted March 29 Share Posted March 29 import React, { useEffect, useRef } from "react"; import gsap from "gsap"; import { ScrollTrigger } from "gsap/ScrollTrigger"; gsap.registerPlugin(ScrollTrigger); export default function TEST() { const boxRef = useRef(null); const oneRef = useRef(null); const twoRef = useRef(null); const threeRef = useRef(null); const fourRef = useRef(null); useEffect(() => { // Register ScrollTrigger // Animation for rotation at each section const rotateAnimation = (trigger, rotation) => { gsap.fromTo(boxRef.current, {rotation:0},{ rotation: rotation, scrollTrigger: { trigger: trigger, start: "top top", end: "bottom top", scrub: true, }, duration:2, }); }; // Apply animations for each section rotateAnimation(oneRef.current, 90); rotateAnimation(twoRef.current, 180); rotateAnimation(threeRef.current, 270); rotateAnimation(fourRef.current, 360); }, []); return ( <div className="h-screen w-screen overflow-scroll overflow-x-hidden scrollbar-thin"> <div ref={boxRef} className="box top-96 left-96 fixed h-14 w-14 bg-black" ></div> <div ref={oneRef} className="one h-screen w-full bg-white"></div> <div ref={twoRef} className="two h-screen w-full bg-slate-600"></div> <div ref={threeRef} className="three h-screen w-full bg-red-600"></div> <div ref={fourRef} className="four h-screen w-full bg-yellow-600"></div> </div> ); } Link to comment Share on other sites More sharing options...
GSAP Helper Posted March 29 Share Posted March 29 Hi @SauravKS and welcome to the GSAP Forums! 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 More sharing options...
SauravKS Posted April 3 Author Share Posted April 3 The code is still not working when using useGSAP instead of useEffect import React, { useEffect, useRef } from "react"; import gsap from "gsap"; import { ScrollTrigger } from "gsap/ScrollTrigger"; import { useGSAP } from "@gsap/react"; gsap.registerPlugin(ScrollTrigger); gsap.registerPlugin(useGSAP); export default function TEST() { const boxRef = useRef(null); const oneRef = useRef(null); const twoRef = useRef(null); const threeRef = useRef(null); const fourRef = useRef(null); useGSAP(() => { gsap.to(boxRef.current, { x: rotation, scrollTrigger: { trigger: trigger, start: "top top", end: "bottom top", scrub: true, }, duration: 2, }); rotateAnimation(oneRef.current, 90); rotateAnimation(twoRef.current, 180); rotateAnimation(threeRef.current, 270); rotateAnimation(fourRef.current, 360); }, []); return ( <div className="h-screen w-screen overflow-scroll overflow-x-hidden scrollbar-thin"> <div ref={boxRef} className="box top-96 left-96 fixed h-14 w-14 bg-black" ></div> <div ref={oneRef} className="one h-screen w-full bg-white"></div> <div ref={twoRef} className="two h-screen w-full bg-slate-600"></div> <div ref={threeRef} className="three h-screen w-full bg-red-600"></div> <div ref={fourRef} className="four h-screen w-full bg-yellow-600"></div> </div> ); } Link to comment Share on other sites More sharing options...
GSAP Helper Posted April 3 Share Posted April 3 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: React (please read this article!) Next Svelte Sveltekit Vue Nuxt 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 More sharing options...
GSAP Helper Posted April 3 Share Posted April 3 Your code doesn't look valid. You reference trigger: trigger but there's no such variable. And you're calling rotateAnimation() but there's no such function. A minimal demo that clearly illustrates the issue will almost surely get you a useful answer. 👍 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now