简体   繁体   中英

ReactJS - setState cloned object or forceUpdate?

In my state in React application, I have one object that is organized similar to tree structure. User can modify any node in that tree, from root to leaf. There are 3 levels in tree (depth = 3), and each level is rendered with specific function. When user update data, in order to re-render, I have two options:

  • Clone this object to another new object, using strategy like DFS to traverse whole tree and find out the node needs modified, then setState this object.

  • Directly mutate the node insides render function, then call forceUpdate from most-top components.

Are there any comparison between above methods or other ideal solutions?

Clone this object to another new object, using strategy like DFS to traverse whole tree and find out the node needs modified, then setState this object.

Seems like the best option for you. This way every time the state that you're modifying changes, your component would re-render without needing anything extra. Here and here you have articles in witch is explain why you never have to use forceUpdate .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM