Last active
March 14, 2019 12:31
-
-
Save stevieoj/4601724bf0ee5c6f1ae03e8113a27339 to your computer and use it in GitHub Desktop.
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
// Create a React HOC that abstracts the selection logic from | |
// https://codepen.io/anon/pen/vPXyOo | |
const withSelector = (ComposedComponent) => ( | |
class extends React.Component { | |
state = { selected: false }; | |
handleSelect = () => { | |
this.setState({ selected: !this.state.selected }); | |
}; | |
render() { | |
// Alternatively we can remove the div wrapper and pass the select handler | |
// function down to the "ComposedComponent". | |
return ( | |
<div onClick={this.handleSelect}> | |
<ComposedComponent | |
selected={this.state.selected} | |
{...this.props} | |
/> | |
</div> | |
); | |
}; | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment