25

I have a react component which hold method like:

mouseEnter(){
    console.log("this is mouse enter")
}

render(){
    var album_list;
    const {albums} = this.props
    if(albums.user_info){
        album_list = albums.user_info.albums.data.filter(album => album.photos).map((album => {
            return
                <div className={"col-sm-3"} key={album.id}     onMouseEnter={this.mouseEnter}>
                    <div className={(this.state.id === album.id) ? 'panel panel-default active-album' : 'panel panel-default'} key={album.id} onClick={this.handleClick.bind(this, album.id)}>
                        <div className={"panel-heading"}>{ album.name }</div>
                        <div className={"panel-body"}>
                            <img className={"img-responsive center-block"} src={album.photos.data[0].source} />
                        </div>
                    </div>
                </div>
            }))
        }
        return (
            <div className={"container"}>
                <div className="row">
                    {album_list}
                </div>
            </div>
        )
    }
}

Here I have onMouseEnter on album_list. When it is hover or mouse enter I want to dispalay a button on that div.

How can I do that ??

Thank you

Peyman Mohamadpour
  • 17,954
  • 24
  • 89
  • 100
varad
  • 7,309
  • 20
  • 60
  • 112

3 Answers3

41

Update the component's state to reflect whether the mouse is inside the component, then use the state value to conditionally render a button.

getInitialState() {
  return {
    isMouseInside: false
  };
}
mouseEnter = () => {
  this.setState({ isMouseInside: true });
}
mouseLeave = () => {
  this.setState({ isMouseInside: false });
}
render() {
  return (
    <div onMouseEnter={this.mouseEnter} onMouseLeave={this.mouseLeave}>
      {this.state.isMouseInside ? <button>Your Button</button> : null}
    </div>
  );
}

Inside the render function we use the conditional operator (?) to return the button component if this.state.isMouseInside is truthy.

Dan Prince
  • 29,491
  • 13
  • 89
  • 120
  • 9
    Looks like onMouseExit API has changed to onMouseLeave https://facebook.github.io/react/docs/events.html#mouse-events – Brian Lenoski May 12 '17 at 17:22
10

There is another approach that uses a reusable render component that would make components 'hoverable' or 'revealable' - whatever makes sense.

class Hoverable extends Component {
  constructor() {
    super();
    this.state = {
      isMouseInside: false
    };
  }

  mouseEnter = () => {
    this.setState({ isMouseInside: true });
  }

  mouseLeave = () => {
    this.setState({ isMouseInside: false });
  }

  render() {
    return this.props.children(
      this.state.isMouseInside, 
      this.mouseEnter, 
      this.mouseLeave
    )
  }
}

Then create the functional component that represents the hoverable element. E.g an album

const HoverableElement = props => (
  <Hoverable>
    {(isMouseInside, mouseEnter, mouseLeave) => (
      <div className="menu-item"> 
        <div onMouseEnter={mouseEnter} onMouseLeave={mouseLeave}>
          <h2>{props.title}</h2>
        </div>
        {isMouseInside && props.children}
      </div>
    )}
  </Hoverable>
)

Finally, use the HoverableElement to render a list of elements that will each be 'hoverable' e.g an array of albums

class HoverableElementsList extends Component {
  render() {
    return (
      <div>
        <HoverableElement title="First Menu">
          <p>Some children content</p>
        </HoverableElement>
      </div>
    )
  }
}
cjjenkinson
  • 359
  • 4
  • 7
0

you can use hooks in the new version of React[>= 16.8.]. Modifying your code it should be:

function YourComponent({info}) {
    
        const [hovered, setIsHovered] = useState(false);
        const handleMouseEnter = () => {
            console.log('handleMouseEnter');
            setIsHovered(true);
        };

        const handleMouseLeave = () => {
            console.log('handleMouseLeave');
            setIsHovered(false);
        };
       
        // ... here is your code mentioned above!
            return (
                <div className={"container"}>
                    <div
                        className="row"
                        onMouseEnter={handleMouseEnter}
                        onMouseLeave={handleMouseLeave}
                    >
                        {album_list}
                    </div>
                    { hovered && (<div>your button</div>)}
                </div>
            )
    }