Setting state on componentDidMount()

后端 未结 3 1842
轮回少年
轮回少年 2020-12-04 12:17

I know that it is an anti-pattern to set state on componentDidMount and a state should be set on componentWillMount but suppose I want to set the l

3条回答
  •  北荒
    北荒 (楼主)
    2020-12-04 12:30

    The only reason that the linter complains about using setState({..}) in componentDidMount and componentDidUpdate is that when the component render the setState immediately causes the component to re-render. But the most important thing to note: using it inside these component's lifecycles is not an anti-pattern in React.

    Please take a look at this issue. you will understand more about this topic. Thanks for reading my answer.

提交回复
热议问题