合并状态更新

时间:2018-03-05 23:16:23

标签: reactjs react-redux

我读了反应文件:https://reactjs.org/docs/state-and-lifecycle.html#state-updates-are-merged

代码在这里:

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


  componentDidMount() {
    fetchPosts().then(response => {
      this.setState({
        posts: response.posts
      });
    });

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

为什么this.setState({comments})保留this.state.posts完整,但完全取代this.state.comments?为什么this.state.posts没有改变?

0 个答案:

没有答案