React - Changing the state without using setState: Must avoid it?

后端 未结 4 2008
轮回少年
轮回少年 2020-12-19 02:55

My code works, but I have a best practice question: I have an array of objects in the state, and a user interaction will change a value of one object at a time. As far a

4条回答
  •  北荒
    北荒 (楼主)
    2020-12-19 03:16

    Muting the state directly breaks the primary principle of React's data flow (which is made to be unidirectional), making your app very fragile and basically ignoring the whole component lifecycle.

    So, while nothing really stops you from mutating the component state without setState({}), you would have to avoid that at all costs if you want to really take advantage of React, otherwise you would be leapfrogging one of the library's core functionalities.

提交回复
热议问题