Why can't I directly modify a component's state, really?

前端 未结 7 836
感动是毒
感动是毒 2020-11-21 22:40

I understand that React tutorials and documentation warn in no uncertain terms that state should not be directly mutated and that everything should go through setState

7条回答
  •  青春惊慌失措
    2020-11-21 23:15

    My current understanding is based on this and this answer:

    IF you don't use shouldComponentUpdate or any other lifecycle methods (like componentWillReceiveProps, componentWillUpdate, and componentDidUpdate) where you compare the old and new props/state

    THEN

    its fine to mutate state and then call setState(), otherwise it is not fine.

提交回复
热议问题