React DOM events on components

I wanted to show or hide a little panel based on the mouse hover status.

When I hovered a link, the panel would show up.

Then I could enter this panel with the mouse, and when I moved the mouse away, the panel would hide.

Like the Twitter p…


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com

I wanted to show or hide a little panel based on the mouse hover status.

When I hovered a link, the panel would show up.

Then I could enter this panel with the mouse, and when I moved the mouse away, the panel would hide.

Like the Twitter profile that shows when you move the mouse upon the name of a person:

on the element that triggered the panel to show up, I added the event onMouseEnter:

<a
  onMouseEnter={() => {
    setShowCard(true)
  }}
>flavio</a>

so the panel would show when I hovered it with the mouse, because it was shown depending on the showCard state variable I had set before:

const [showCard, setShowCard] = useState(false)

Then I had the ProfileCard component but I couldn’t just do:

<ProfileCard
onMouseEnter={() => {
  setShowCard(true)
}}
onMouseLeave={() => {
  setShowCard(false)
}}
/>

because it didn’t work. ProfileCard is not a DOM element, so it didn’t get the events fired to respond to.

What I had to do was to pass onMouseEnter and onMouseLeave as props to the ProfileCard component, then identify the correct DOM element inside it that could receive those events, and attach the event handler there. In this case, I used the container div:

const ProfileCard = ({
  onMouseEnter,
  onMouseLeave
}) => (     
  <div
    onMouseEnter={onMouseEnter}
    onMouseLeave={onMouseLeave}>
      ...

Now leaving the panel would hide it.


This content originally appeared on
flaviocopes.com and was authored by flaviocopes.com


Print Share Comment Cite Upload Translate Updates
APA

flaviocopes.com | Sciencx (2021-07-16T05:00:00+00:00) React DOM events on components. Retrieved from https://www.scien.cx/2021/07/16/react-dom-events-on-components/

MLA
" » React DOM events on components." flaviocopes.com | Sciencx - Friday July 16, 2021, https://www.scien.cx/2021/07/16/react-dom-events-on-components/
HARVARD
flaviocopes.com | Sciencx Friday July 16, 2021 » React DOM events on components., viewed ,<https://www.scien.cx/2021/07/16/react-dom-events-on-components/>
VANCOUVER
flaviocopes.com | Sciencx - » React DOM events on components. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/07/16/react-dom-events-on-components/
CHICAGO
" » React DOM events on components." flaviocopes.com | Sciencx - Accessed . https://www.scien.cx/2021/07/16/react-dom-events-on-components/
IEEE
" » React DOM events on components." flaviocopes.com | Sciencx [Online]. Available: https://www.scien.cx/2021/07/16/react-dom-events-on-components/. [Accessed: ]
rf:citation
» React DOM events on components | flaviocopes.com | Sciencx | https://www.scien.cx/2021/07/16/react-dom-events-on-components/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.