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
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".
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
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
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".