阅读文档的一个关于状态合并的疑问

#1
  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}) leaves this.state.posts intact, but completely replaces this.state.comments?

为什么this.state.posts 会完整留下,而this.state.comments 会被覆盖?

谢谢大家。

#2

我理解错了。。。我看懂了。。。管理员可以删除这个贴

#3

这个方面我也看到,我看的中文版本的,是说所以 this.setState({comments}) 不会改变 this.state.posts 的值,但会完全替换this.state.comments 的值 但是应该也会完全替换this.state.post的值吧,还是中文版翻译有误,不甚理解

#4

@jiasber

我看得是英文版的文档。翻译应该是对的。

我理解错了。

最后那段话意思是说

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

这个语句不会影响posts的值,只会替换comment的值。

我想多了,以为上下setState的顺序也有关系。

#5

…听了你的解释我明白了,我理解错了…(┬_┬),感谢