Jump to content
Search Community

Looping through Nav items and applying animation in React

TaiwoJazz test
Moderator Tag

Go to solution Solved by Rodrigo,

Recommended Posts

  • Solution

Hi,

 

Is better to create the animation on each iteration but just keep one instance of GSAP Context in your component, otherwise it can get a bit messy.

 

This seems to work the way you intend:

https://codesandbox.io/s/hover-on-nav-element-react-forked-ydjc1d?file=/src/Nav.jsx

 

Just store a timeline instance for each link on an array that is kept through re-renders (useRef) and, since you're mapping your links obviously you'll have the same number of links and animations, you can pass that index to the mouse enter/leave handlers. In fact if you want even more abstraction you can create just a single event handler that toggles the reversed state of the particular target:

https://codesandbox.io/s/hover-on-nav-element-react-forked-7hy179?file=/src/Nav.jsx

 

Hopefully this helps,

 

Happy Tweening!

  • 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.
×
×
  • Create New...