Jump to content
Search Community

How to use mouseEnter and mouseLeave with this helper function

TaiwoJazz test
Moderator Tag

Recommended Posts

const ctx = useRef();

// Event Handler
const clickEventHandler = () => {
  ctx.current.onClick();
};

useLayoutEffect(() => {
  ctx.current = gsap.context((self) => {
    // use any arbitrary string as a name; it'll be added to the Context object, so in this case you can call onClick() later...
    self.add("onClick", (e) => {
      gsap.to(...); // <-- gets added to the Context!
    });

  }, myRef);
  
  return () => ctx.current.revert();
}, []);

Turns out that this helper function will do the trick i've been struggling to work around but i need it to be mouseEnter and mouseLeave... and i also want to loop through the items i'm hovering over... Thanks in advance...

Link to comment
Share on other sites

Hi,

 

Is really hard to see what could be the issue without a minimal demo, so if you could provide one it would be great.

 

The only advice I can offer in based on the snippet you provided is to use React's events handler and just execute the method you stored in the context, something like this:

const ctx = useRef();

// Event Handler
const mouseEnterHandler = () => {
  ctx.current.onMouseEnter();
};

useLayoutEffect(() => {
  ctx.current = gsap.context((self) => {
    // use any arbitrary string as a name; it'll be added to the Context object, so in this case you can call onClick() later...
    self.add("onMouseEnter", (e) => {
      gsap.to(...); // <-- gets added to the Context!
    });

  }, myRef);
  
  return () => ctx.current.revert();
}, []);

return (
  <div>
    {myElements.map((element) =>
      <div key={element.id} onMouseEnter={mouseEnterHandler}>{element.content}</div>
    )}
  </div>
);

Happy Tweening!

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...