React show button on mouse enter. Update the component’s state to reflect whether the mouse is inside the component, then use the state value to conditionally render a button.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | 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.
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.