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

后端 未结 4 1994
轮回少年
轮回少年 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:03

    If I understood your question right, you have an array of objects and when a property of a single object in array changes,

    1. Create a deep clone of the array and pass to setState
    2. Create a shallow clone and pass to setState

    I just checked with the redux sample todo app and in case of a single property of an object changes you've to create a fresh copy of that single object not the entire array. I recommend you to read about redux and if possible use to manage the state of your app.

提交回复
热议问题