Last active
December 22, 2017 18:55
-
-
Save ArunMichaelDsouza/1c9ca92ca79f0e84250e42b3b6630e75 to your computer and use it in GitHub Desktop.
Updating state of a parentless component in React
This file contains 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
// 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