Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
96
rated 0 times [  102] [ 6]  / answers: 1 / hits: 39911  / 8 Years ago, tue, march 1, 2016, 12:00:00

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


More From » reactjs

 Answers
61

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.


[#63104] Saturday, February 27, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
zariahdiamondz

Total Points: 649
Total Questions: 109
Total Answers: 88

Location: Tajikistan
Member since Thu, Apr 14, 2022
2 Years ago
;