Skip to content

Instantly share code, notes, and snippets.

@du5rte
Forked from laem/HoverDecorator.jsx
Last active May 8, 2017 10:45
Show Gist options
  • Save du5rte/95c85e1a4061e6e07a3826bd2a637ba3 to your computer and use it in GitHub Desktop.
Save du5rte/95c85e1a4061e6e07a3826bd2a637ba3 to your computer and use it in GitHub Desktop.
React Hover Component Decorator
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