setState is not updating state properly

后端 未结 4 1952
忘掉有多难
忘掉有多难 2021-01-21 09:31

Sorry, I really miss something with the transmission of state within props of sub components in React.

I have implemented a version of a todo l

4条回答
  •  无人及你
    2021-01-21 10:19

    As the other answers stated, the Array push method does not return the array. Just to complement the answers above, if you are using ES6, a nice and elegant way of doing this is using the spread operator (you can read more about it here)

    this.setState({
        tasks: [...this.state.tasks, task]
    })
    

    It is essentially the same as using the concat method, but I think this has a nicer readability.

提交回复
热议问题