Why not one more answer? setState()
and the setState()
-triggered render()
have both completed executing when you call componentDidMount()
(the first time render()
is executed) and/or componentDidUpdate()
(any time after render()
is executed). (Links are to ReactJS.org docs.)
Example with componentDidUpdate()
Caller, set reference and set state...
<Cmp ref={(inst) => {this.parent=inst}}>;
this.parent.setState({'data':'hello!'});
Render parent...
componentDidMount() { // componentDidMount() gets called after first state set
console.log(this.state.data); // output: "hello!"
}
componentDidUpdate() { // componentDidUpdate() gets called after all other states set
console.log(this.state.data); // output: "hello!"
}
Example with componentDidMount()
Caller, set reference and set state...
<Cmp ref={(inst) => {this.parent=inst}}>
this.parent.setState({'data':'hello!'});
Render parent...
render() { // render() gets called anytime setState() is called
return (
<ChildComponent
state={this.state}
/>
);
}
After parent rerenders child, see state in componentDidUpdate()
.
componentDidMount() { // componentDidMount() gets called anytime setState()/render() finish
console.log(this.props.state.data); // output: "hello!"
}