Created
November 7, 2015 19:07
-
-
Save dlmanning/c400722f14177f7638de to your computer and use it in GitHub Desktop.
Simple higher-order React component to make a compoent react to hover events.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function hoverable (WrappedComponent, propName = 'hover') { | |
return class HoverableComponent extends Component { | |
constructor (props) { | |
super(props) | |
this.state = { hovered: false } | |
} | |
turnHoverOn () { | |
this.setState({ hovered: true }) | |
} | |
turnHoverOff () { | |
this.setState({ hovered: false }) | |
} | |
render () { | |
const props = { [propName]: this.state.hovered, ...this.props } | |
return ( | |
<div onMouseEnter={() => this.turnHoverOn()} onMouseLeave={() => this.turnHoverOff()}> | |
<WrappedComponent { ...props } /> | |
</div> | |
) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment