Last active
May 8, 2017 10:45
-
-
Save laem/c8a224649e2fcaad81b8af99193b7375 to your computer and use it in GitHub Desktop.
React Hover Component Decorator
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
import React, {Component} from 'react' | |
export default DecoratedComponent => | |
class extends Component { | |
state = { | |
hover: false, | |
} | |
toggleHover = () => | |
this.setState({hover: !this.state.hover}) | |
render() { | |
return <span onMouseEnter={this.toggleHover} onMouseLeave={this.toggleHover} > | |
<DecoratedComponent {...this.props} hover={this.state.hover}/> | |
</span> | |
} | |
} | |
@HoverDecorator | |
class Maurice extends Component { | |
render() { | |
let {hover} = this.props, | |
style = {background: hover ? 'blue' : 'red'} | |
return ( | |
<span | |
style={labelStyle}> | |
LALALA | |
</span> | |
) | |
} | |
} | |
// Then in a render method, <Maurice /> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment