-
-
Save du5rte/95c85e1a4061e6e07a3826bd2a637ba3 to your computer and use it in GitHub Desktop.
React Hover Component Decorator
This file contains hidden or 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 function hoverDecorator(InitialComponent) { | |
return class HoverDecorator extends Component { | |
constructor() { | |
super() | |
this.state = { | |
hover: false | |
} | |
} | |
handleMouseEnter() { | |
this.setState({ hover: true }) | |
} | |
handleMouseLeave() { | |
this.setState({ hover: false }) | |
} | |
render() { | |
return ( | |
<InitialComponent | |
{...this.state} | |
onMouseEnter={this.handleMouseEnter.bind(this)} | |
onMouseLeave={this.handleMouseLeave.bind(this)} | |
{...this.props} | |
/> | |
) | |
} | |
} | |
} | |
@HoverDecorator class MyHeader extends Component { | |
render() { | |
const { hover } = this.props | |
const style = { background: hover ? "blue" : "red" } | |
return ( | |
<h1 style={style}> | |
Hello World | |
</h1> | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment