Skip to content

Instantly share code, notes, and snippets.

@dane-stevens
Last active July 10, 2018 21:00
Show Gist options
  • Save dane-stevens/5152eb3fa01f1efb18735b766063eebb to your computer and use it in GitHub Desktop.
Save dane-stevens/5152eb3fa01f1efb18735b766063eebb to your computer and use it in GitHub Desktop.
import React from "react";
import PropTypes from "prop-types";
// Create a State Render Prop Component
class State extends React.Component {
constructor(props) {
super(props);
// Initialize state from init prop
this.state = this.props.init;
this.handleStateChange = this.handleStateChange.bind(this);
}
// Create function to update state
handleStateChange = state => {
this.setState(state);
};
render() {
// Pass handleStateChange and the current state child elements
return (
<React.Fragment>
{this.props.children(this.handleStateChange, this.state)}
</React.Fragment>
);
}
}
State.propTypes = {
// init must be an object
init: PropTypes.object,
// Child function is required
children: PropTypes.func.isRequired
};
export default State;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment