Jump to content
GreenSock

gew

selector on mapped array in next js doesnt trigger animation

Recommended Posts

const MARQUE_DATA = ["WEBSITE", "AR", "MOBILE APP", "IOT", "VR"];

const Marquee = ({ data }: { data: string[] }) => {
  const wrapper = useRef<HTMLDivElement>(null);
  const inner = useRef<HTMLDivElement>(null);
  const q = gsap.utils.selector(inner);

  useEffect(() => {
    gsap.to(q("span"), {
      x: 500,
      duration: 8,
      repeat: -1,
    });
  }, []);

  return (
    <div ref={wrapper} className={styles.marqueeWrapper}>
      <div ref={inner} className={styles.marqueeInner}>
        {data.map((text, i) => (
          <span key={i}>
            {text} |
          </span>
        ))}
      </div>
    </div>
  );
};

the rendered span doesnt animate if i use map like the code above, but when i try to hardcode the spans like 

      <span>FOO</span>
      <span>BAR</span>
      <span>BEAR</span>

this it does work as expected. any help apreciated !

 

 

  • Like 1
Link to comment
Share on other sites

It's pretty tough to troubleshoot without a minimal demo - the issue could be caused by CSS, markup, a third party library, your browser, an external script that's totally unrelated to GSAP, etc. Would you please provide a very simple CodePen or CodeSandbox that demonstrates the issue? 

 

Please don't include your whole project. Just some colored <div> elements and the GSAP code is best (avoid frameworks if possible). See if you can recreate the issue with as few dependancies as possible. If not, incrementally add code bit by bit until it breaks. Usually people solve their own issues during this process! If not, then at least we have a reduced test case which greatly increases your chances of getting a relevant answer.

 

Here's a starter CodePen that loads all the plugins. Just click "fork" at the bottom right and make your minimal demo

See the Pen aYYOdN by GreenSock (@GreenSock) on CodePen

 

If you're using something like React/Next/Nuxt/Gatsby or some other framework, you may find CodeSandbox easier to use. 

 

Once we see an isolated demo, we'll do our best to jump in and help with your GSAP-specific questions. 

  • Like 1
Link to comment
Share on other sites

My guess is that the useEffect is running before your spans are rendered via the map function. I'd put a check in your useEffect to make sure they exist before adding the gsap code. Again, a minimal demo is very helpful to check this sort of stuff.

  • Like 1
Link to comment
Share on other sites

If it is the issue that @elegantseagulls suggests, which it probably is, then I would use a ref array instead to better keep track of when elements exist.

The useArrayRef hook here:

Makes it easy to do so.

 

  • Like 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.
×