Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show a component on hover in reactjs [closed]

Tags:

css

reactjs

I've created several sections with the heading of the specific content.

I want to show a short sneak preview on hovering above the different section.

Does anyone knows how to create a hoverActionHandler with conditional rendering of a react component?

like image 303
R. Meier Avatar asked Jun 15 '17 11:06

R. Meier


People also ask

How do you trigger a hover event in React?

We do this by adding onMouseOver to the button element. After declaring that this element has an onMouseEnter event handler, we can choose what function we want to trigger when the cursor hovers over the element. We declare a function called changeBackground above the view part of the React Component.

How do you show and hide components in React?

To show and hide components and elements in React you will need to either use conditional rendering, css styles or animation libraries. For the most part conditional rendering will be done by checking if a value passes a condition, just like in an if statement but for react components and JSX.

What is onMouseEnter React?

The onMouseOver event in React occurs when the mouse pointer is moved onto an element (it can be a div, a button, an input, a textarea, etc). The event handler function will be fired and we can execute our logic there. The onMouseOut event in React occurs when the mouse pointer is moved out of an element.


1 Answers

You can use onMouseOver and onMouseOut to change the state and render a component conditionally based on the value of the state.

See it in action:

  • Hooks Implentation: https://codesandbox.io/s/react-hover-example-hooks-0to7u
  • Class Implemntation: https://codesandbox.io/s/XopkqJ5oV

Hooks:

import React, { useState } from "react"; import { render } from "react-dom";  const HoverableDiv = ({ handleMouseOver, handleMouseOut }) => {   return (     <div onMouseOver={handleMouseOver} onMouseOut={handleMouseOut}>       Hover Me     </div>   ); };  const HoverText = () => {   return (     <div>       Hovering right meow!       <span role="img" aria-label="cat">         🐱       </span>     </div>   ); };  const HoverExample = () => {   const [isHovering, setIsHovering] = useState(false);   const handleMouseOver = () => {     setIsHovering(true);   };    const handleMouseOut = () => {     setIsHovering(false);   };    return (     <div>       {/* Hover over this div to hide/show <HoverText /> */}       <HoverableDiv         handleMouseOver={handleMouseOver}         handleMouseOut={handleMouseOut}       />       {isHovering && <HoverText />}     </div>   ); }; 

Class:

import React, { Component } from "react"; import { render } from "react-dom";  const HoverableDiv = React.memo(({ handleMouseOver, handleMouseOut }) => {   return (     <div onMouseOver={handleMouseOver} onMouseOut={handleMouseOut}>       Hover Me     </div>   ); });  const HoverText = () => {   return (     <div>       Hovering right meow!       <span role="img" aria-label="cat">         🐱       </span>     </div>   ); };  class HoverExample extends Component {   constructor(props) {     super(props);     this.handleMouseOver = this.handleMouseOver.bind(this);     this.handleMouseOut = this.handleMouseOut.bind(this);     this.state = {       isHovering: false     };   }    handleMouseOver() {     this.setState(() => ({       isHovering: true     }));   }    handleMouseOut() {     this.setState(() => ({       isHovering: false     }));   }    render() {     return (       <div>         {/* <HoverText /> gets shown when mouse is over <HoverableDiv /> */}         <HoverableDiv           handleMouseOver={this.handleMouseOver}           handleMouseOut={this.handleMouseOut}         />         {this.state.isHovering && <HoverText />}       </div>     );   } } 
like image 145
Ben Bud Avatar answered Sep 18 '22 16:09

Ben Bud