fun Posted November 9, 2023 Share Posted November 9, 2023 Hello, I'm Trying to use GSAP with React with this animation.. it works fine with vanilla JS but with React there is huge white space it's EXACT SAME CODE but works differently See the Pen JjxWVLP by noX10M (@noX10M) on CodePen Link to comment Share on other sites More sharing options...
GSAP Helper Posted November 9, 2023 Share Posted November 9, 2023 Perhaps the problem is that React 18 runs in "strict" mode locally by default which causes your useEffect() to get called TWICE! Very annoying. It has caused a lot of headaches for a lot of people outside the GSAP community too. So I bet you're inadvertently creating duplicate ScrollTriggers/tweens that are conflicting. .from() tweens use the CURRENT value as the destination and it renders immediately the value you set in the tween, so when it's called the first time it'd work great but if you call it twice, it ends up animating from the from value (no animation). It's not a GSAP bug - it's a logic thing. For example, let's say el.x is 0 and you do this: useEffect(() => { // what happens if this gets called twice? gsap.from(el, {x: 100}) }, []); The first time makes el.x jump immediately to 100 and start animating backwards toward the current value which is 0 (so 100 --> 0). But the second time, it would jump to 100 (same) and animate back to the current value which is now 100 (100 --> 100)! See the issue? In GSAP 3.11, we introduced a new gsap.context() feature that solves all of this for you. All you need to do is wrap your code in a context call, and then return a cleanup function that reverts things: // typically it's best to useLayoutEffect() instead of useEffect() to have React render the initial state properly from the very start. useLayoutEffect(() => { let ctx = gsap.context(() => { // all your GSAP animation code here }); return () => ctx.revert(); // <- cleanup! }, []); We strongly recommend reading the React article at https://greensock.com/react Happy tweening! Link to comment Share on other sites More sharing options...
fun Posted November 9, 2023 Author Share Posted November 9, 2023 Thank you for your detailed explanation. now it's better but also there is a white space appears in a fraction of second before it work fine 2023-11-09 12-44-53 - Trim - Trim.mp4 Link to comment Share on other sites More sharing options...
GreenSock Posted November 9, 2023 Share Posted November 9, 2023 I watched your video 4 times and I don't see what you're talking about. 🤷♂️ If you're talking about the pinning, you can try any of these: Set anticipatePin: 1 for example Try ScrollTrigger.normalizeScroll(true) If you still need help, please make sure you provide a minimal demo that clearly illustrates the problem and explain how we can reproduce it. 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