Skip to content

Instantly share code, notes, and snippets.

@ArunMichaelDsouza
Last active December 22, 2017 18:55
Show Gist options
  • Save ArunMichaelDsouza/1c9ca92ca79f0e84250e42b3b6630e75 to your computer and use it in GitHub Desktop.
Save ArunMichaelDsouza/1c9ca92ca79f0e84250e42b3b6630e75 to your computer and use it in GitHub Desktop.
Updating state of a parentless component in React
// Child.jsx
class Child extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
<button
type="button"
onClick={ this.props.updateParent }
>
Update Parent
</button>
);
}
}
// Parent.jsx
class Parent extends React.Component {
constructor(props) {
super(props);
this.state = {
shown: true
};
}
updateState() {
this.setState({ shown: false });
}
render() {
return (
<Child
updateParent={ this.updateState.bind(this) }
/>
);
}
}
ReactDOM.render(<Parent />, document.getElementById('root'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment