I used gsap.fromto animate a component when it is rendered through a conditional statement
something like below:
const [openComponent, setOpenComponent] = useState(false);
<button onClick={_=>setOpenComponent(true)}>Open</button>
return {openComponent && <Component/>}
and in the Component.jsI used useEffectto render the animation
useEffect(_ => {
gsap.set(ref.current, { y: 0 });
gsap.from(ref.current, {
y: "-100%",
duration: 0.5,
ease: "expo.out",
});
}, []);
However, the Componentsometimes rendered in its original position (y=0) before starting the animation. It means you will see a flick of the component before the animation starts.
When I changed useEffectto useLayoutEffect, it doesn't happen.
I have another component called Loading which uses a similar logic as above and the flick doesn't happen. I went through the forum and found this post
It is true that the Component has a React.fragment (Loading doesn't have React.fragment)but when I removed it, the flick still happens unless I use useLayoutEffect.
Another difference of the Loading from the Component is that Loading is less complicated.
Component structure:
<div>
<div>
<div>
<p></p>
<div>
<span></span>
<span></span>
</div>
</div>
<div>
<p></p>
</div>
<div>
<p></p>
</div>
</div>
</div>
Loading structure:
<div>
<img/>
</div>
Here is a video example (Ignore the watermark?)
issue-2_sMe3OB4K_Skcz.mp4
I am not familiar with codepen or any other online editors to code a React in it and make an example. Sorry for the inconvenience. ??