Skip to content

Instantly share code, notes, and snippets.

@JavascriptMick
Created February 29, 2016 21:55
Show Gist options
  • Save JavascriptMick/0417a99bb932bbacf5e6 to your computer and use it in GitHub Desktop.
Save JavascriptMick/0417a99bb932bbacf5e6 to your computer and use it in GitHub Desktop.
Use a Higher order component to add mouse events to a host component
import React from "react";
export default function GetNasty(TargetComponent){
return class GetNasty extends React.Component{
handleOnClick($event){
alert("You suck!");
}
render(){
const props = Object.assign({
onClick: this.handleOnClick
}, this.props);
return (<TargetComponent {...props}/>);
}
}
}
import React from "react";
import GetNasty from "./GetNasty";
class Button extends React.Component{
render(){
return (
<button {...this.props}>click me</button>
);
}
}
const NastyButton = GetNasty(Button);
export default NastyButton;
@JavascriptMick
Copy link
Author

I'm not really happy with the use of {..this.props} on the render method of the host component (Button)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment