Last active
July 10, 2018 21:00
-
-
Save dane-stevens/5152eb3fa01f1efb18735b766063eebb 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
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