DEV Community

[Comment from a deleted post]
Collapse
 
tobiasjacob profile image
Tobias Jacob

I don't think this is a good example. The fault is not, that react cannot look for array changes, but instead that you mutated your state.

NEVER MUTATE THE STATE

In the example above, this means, that you have to create a whole new array with whole new objects for react to pick up the changes. For example

const [data, setData] = useState([])

In another hook:
function capitalizeAllRows() {
setData(data.map((row) => ({...row, name: Capitalize(row.name)})))
}

note that with the spread operator, we created a new object.

I still like the idea of using hashs (for example json.stringify, even tough there are more performant ways) for applying effects if an array changes. Just the example is not well chosen

Collapse
 
mvozaar profile image
mvozaar

example is good but not for solving the problem. but for detection and verification of root cause is good :). it saves your time if you want to find problem. right solution for state changes is as you mentioned "NEVER MUTATE THE STATE".