I am sending state value from parent component to child component as props, but I child component updates before the parent component's state is set.
This is in the parent component
handleSubmit = function(e){
e.preventDefault();
this.setState({table: true});
this.setState({rows: [
{
id: 1,
name: this.state.name,
email: this.state.email,
phone: this.state.phone
}
]
} , () => {console.log(this.state.rows)});
this.setState({updateInTable: true} , () => {console.log(this.state.updateInTable)})
this.setState({submit: false});
}
This is in the child component
componentDidUpdate() {
console.log('From table '+this.props.updateInTable);
if(this.props.updateInTable){
console.log('Coming from table '+this.props.rows);
}
}
I am sending these as props
<Table
table={this.state.table}
name={this.state.name}
rows={this.state.rows}
updateInTable={this.state.updateInTable}
/>
These are returning from console
From table false
From table true
Coming from table [object Object]
[Object]
true
I expect [Object], true which are in parent component to be printed first and then From table false , From table true
CodePen Link: https://codepen.io/anon/pen/ZXOrPE