r/reactjs•
outc4sted

Wondering if I'm updating state correctly when the data is a tree structure.

Code Review Request

Hi all, I was helping someone else in the reactiflux discord where they were trying to update the state of a tree structure. I was wondering if I went about it the right way when assigning new data.

https://codesandbox.io/s/polished-moon-qb67rc?file=/src/App.jsx

Inside the setData function (from useState) I traverse the tree and then add the new data to the existing array at the node using the spread operator. Is there a better way?

Posted Nov 11, 2022, 8:39 PM UTC

41
Loading comments…