Le Duong Tuan Anh
Le Duong Tuan Anh

Reputation: 360

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:

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

Upvotes: 2

Views: 161

Answers (1)

Alberto Perez
Alberto Perez

Reputation: 2922

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.

Upvotes: 0

Related Questions