LucieB Posted June 9, 2020 Posted June 9, 2020 Hey my friends! I'm working with React and I use GSAP for the first time with Contexts. Perhaps i mistaken (because of the refs and useEffects hooks?), but my timeline's working twice all the time, and I don't know why. I tried to use from, fromTo, to... And actually every time, the issue is present. I created a short CodeSandBox to illustrate my issue. Of course, for my example I don't need the use the Context, but in my real project I would. Hope you guys are fine in these strange moments. Have a nice day! Lucie
OSUblake Posted June 9, 2020 Posted June 9, 2020 Not sure what's supposed to happen based on your code, but your timeline keeps getting longer because you keep adding to it everytime that useEffect function gets called. If you plan on reusing your timeline, you should create your animations inside another useEffect. const tl = useRef(null); // don't create timelines inside a ref declaration useEffect(() => { const popContainer = popContainerRef.current; const popTitle = popContainer.children[1]; // create timeline here tl.current = gsap.timeline({ paused: true }) .to(popcontainer, { ... }); // return function to kill timeline on dismount return () => tl.kill(); }, []); useEffect(() => { // If status is TRUE -> play the timeline status && tl.current.play(); }, [status]); 3 1
LucieB Posted June 9, 2020 Author Posted June 9, 2020 You're the best! Now it's working as I want: THANK YOU! 1
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