React & Modern Frontend (445 Qs)•Part 11: Legacy Architecture, Migration Patterns & Historical Internals (354 - 445)
How do you say that state updates are merged?
When you call setState() in the component, React merges the object you provide into the current state.
vbnet
For example, let us take a facebook user with posts and comments details as state variables,
javascript
constructor(props) {
super(props);
this.state = {
posts: [],
comments: []
};
}
scss
Now you can update them independently with separate `setState()` calls as below,
javascript
componentDidMount() {
fetchPosts().then(response => {
this.setState({
posts: response.posts
});
});
fetchComments().then(response => {
this.setState({
comments: response.comments
});
});
}
go
As mentioned in the above code snippets, `this.setState({comments})` updates only comments variable without modifying or replacing `posts` variable.