Jump to content
Search Community

Why is the animation not working?

AlexanderGS test
Moderator Tag

Go to solution Solved by GreenSock,

Recommended Posts

When the page is reloaded, the animation is not performed. What is the problem?

 

image.png.8f0b72d3841cc16a6835dafc717e468f.png

 

const array = [
  {
    title: "Ref Element 1"
  },
  {
    title: "Ref Element 2"
  },
  {
    title: "Ref Element 3"
  }
];

export function Slider() {
  const titleH1Refs = useRef([]);
  titleH1Refs.current = [];

  //checking for an existing element in an array
  const addToRefsTitleH1 = el => {
    if (el && !titleH1Refs.current.includes(el)) {
      titleH1Refs.current.push(el);
    }
  };
  
  //perform animation for all array elements
  useLayoutEffect(() => {
    titleH1Refs.current.forEach((element) => {
      gsap.from(element, {
        opacity: 0,
        y: 20,
        ease: Expo.easeInOut
      })
    })
  }, [titleH1Refs.current]);

  return (
    <section className="container">
      {array.map((element) => (
          <div className="element" ref={addToRefs}>{element.title}</div>    
        ))}  
    </section>
  );
}

 

Link to comment
Share on other sites

  • Solution

Welcome to the forums, @AlexanderGS

 

The problem is that React 18 calls your useEffect() TWICE in strict mode! Very annoying. It has caused a lot of headaches for a lot of people outside the GSAP community too.

 

.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. It makes cleanup super easy. 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!
}, []);

 

I'd strongly recommend reading the React article at

 

 

So in your demo... 

// OLD
useLayoutEffect(() => {
  titleH1Refs.current.forEach((element) => {
    gsap.from(element, {
      opacity: 0,
      y: 20,
      ease: Expo.easeInOut
    })
  })
}, [titleH1Refs.current]);


// NEW
useLayoutEffect(() => {
  let ctx = gsap.context(() => {
    gsap.from(titleH1Refs.current, {
      opacity: 0,
      y: 20,
      ease: "expo.inOut"
    });
  });
  return () => ctx.revert(); // <-- cleanup!
  }, [titleH1Refs.current]);

You don't need to loop through each element and create a separate tween for each one. You can feed them all into one. It's totally fine if you prefer that loop - it's just not necessary. You might also want to look into the selector scoping in gsap.context() because it can save you from the hassle of creating/managing a bunch of Refs. 

 

Enjoy!

  • Like 2
  • Thanks 1
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...