Question

What does it mean that the merge that setState() does is shallow?

#react

Answer

constructor(props) {
  super(props);
  this.state = {
    posts: [],
    comments: []
  };
}

componentDidMount() {
  fetchComments().then(response => {
    this.setState({ comments: response.comments });
  });
}

It means that this.setState({ comments }) above leaves this.state.posts intact, but completely replaces this.state.comments.

https://reactjs.org/docs/state-and-lifecycle.html#state-updates-are-merged

Learn

Author

xavi

Reviews
Last
next

Students 1