Skip to content

Instantly share code, notes, and snippets.

@stevieoj
Last active March 14, 2019 12:31
Show Gist options
  • Save stevieoj/4601724bf0ee5c6f1ae03e8113a27339 to your computer and use it in GitHub Desktop.
Save stevieoj/4601724bf0ee5c6f1ae03e8113a27339 to your computer and use it in GitHub Desktop.
// 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