Skip to content

Instantly share code, notes, and snippets.

@stephenjwatkins
Created August 9, 2018 20:10
Show Gist options
  • Save stephenjwatkins/7bd615ba64fd74c2018a7a6e024e44c5 to your computer and use it in GitHub Desktop.
Save stephenjwatkins/7bd615ba64fd74c2018a7a6e024e44c5 to your computer and use it in GitHub Desktop.
Generic State component for React.
import React from "react";
import isFunction from "lodash/isFunction";
class State extends React.Component {
constructor(...args) {
super(...args);
this.boundSetState = this.setState.bind(this);
this.state = isFunction(this.props.initialState)
? { ...this.props.initialState(this.boundSetState) }
: { ...this.props.initialState };
}
render() {
return this.props.children(this.state, this.boundSetState);
}
}
export { State };
@stephenjwatkins
Copy link
Author

Probably best to use react-powerplug instead.

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