简体   繁体   English

ReactJS-setState克隆的对象还是forceUpdate?

[英]ReactJS - setState cloned object or forceUpdate?

In my state in React application, I have one object that is organized similar to tree structure. 在React应用程序的状态下,我有一个对象的组织类似于树结构。 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. 树中有3个级别(深度= 3),每个级别都使用特定功能进行渲染。 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. 将该对象克隆到另一个新对象,使用DFS之类的策略遍历整棵树,找出需要修改的节点,然后使用setState这个对象。

  • Directly mutate the node insides render function, then call forceUpdate from most-top components. 直接更改渲染函数内部的节点,然后从最顶层的组件调用forceUpdate。

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. 这样,每次您要修改的state发生变化时,组件都将重新渲染而无需任何额外的操作。 Here and here you have articles in witch is explain why you never have to use forceUpdate . 您在这里这里的《女巫》中的文章都解释了为什么不必使用forceUpdate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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