React Core & Hooks (407 Qs)•Part 9: React Hooks Deep Dive (262 - 316)
How do you say that state updates are merged?
mid LevelCode ExampleHooks
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.