Skip to content
Advertisement

How to get the rect.top and rect.left of an element if it’s mapped in React JS

I am trying to have a gloss effect on some cards when the mouse moves over them.

What I need is to move the gloss effect base on the mouse position. The problem that I have is that I can’t get the top and left of the card because they are mapped like this

JavaScript

I have tried to use the index to get the rect of the element but it doesn’t work. It’s saying that GlowingCardsGrid.jsx:21 Uncaught TypeError: index.getBoundingClientReact is not a function.

JavaScript

Is there a way I could get the top and left of each element depending on which one I’m hovering on?

Advertisement

Answer

If you just want to change some styles or classes when an element is hovered, the better approach would be to create a reusable component that could manage its own state.

Using onMouseOver to set a state variable to true and onMouseOut to set it back to false gives you the ability to do whatever you want (e.g., run an animation, add some styles or change the classes) when the element is hovered.

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement