Can't perform a React state update on an unmounted component?

后端 未结 2 1951
悲哀的现实
悲哀的现实 2021-01-22 06:19

I am fetching data in componentDidMount and updating the state and the famous warning is appearing:

Can\'t perform a React state update on an unmounted co

2条回答
  •  时光取名叫无心
    2021-01-22 07:05

    Just create flag in state: isMounted: false. In componentDidMount set state isMounted = true and in componentWillUnmount: set state isMounted = false.

    And in the async function that need use setState function, wrap them by condition if(this.state.isMounted) { ....setState action }

提交回复
热议问题