javascript - 如何防止我的数组在 React JS 中的 onChange 函数上一次又一次地更新?
问题描述
我已经克隆了一个数组并且我有一个onChange
函数。在调用onChange
函数时,我的原始和克隆数组正在根据更新的 onChange 值进行更新。但我不想更新我的克隆数组。我怎样才能做到这一点 ?
我的代码 -
const clonedArray = [...originalArray];
const onChange = (id:number, value: string): void => {
const arrayData = originalArray;
const selectedData = arrayData.find(
(data) => data.myId === id
);
if (selectedData) {
// updating my originalArray according to new changed value;
}
}
这个onChange
功能也在更新我的clonedArray
. 我不想更新我的clonedArray
. 我怎样才能做到这一点 ?useMemo 什么的解决方案是什么?谁能告诉我解决方案?
解决方案
Issue
This issue is state object/array mutation. You are mutating elements in an array and seeing the mutations manifest in a "copy".
Firstly, const clonedArray = [...originalArray];
is only a shallow copy of the originalArray
, not a clone. This means that other than the array reference itself, all the elements in clonedArray
still refer to the same elements in originalArray
.
Secondly, if later you are making changes in originalArray
and you are seeing them manifest in clonedArray
then you are definitely mutating the element references instead of creating new references.
Solution
You are looking for the Immutable Update Pattern. When updating state in React it is necessary to shallow copy not only the root object/array that is being updated, but all nested state as well. To help with endeavor, especially when updating arrays, you will want to also use functional state updates so you can correctly update from the previous state.
For this I'm assuming (based on a comment that originalArray
was in state) that your state looks something like this:
const [originalArray, setOriginalArray] = useState([]);
The change handler/state update (just an example since I don't know your exact update requirements)
const onChange = (id: number, value: string): void => {
const selectedData = originalArray.find((data) => data.myId === id);
if (selectedData) {
// updating my originalArray according to new changed value
// Array.prototype.map shallow copies the array into a new array reference
setOriginalArray(originalArray => originalArray.map(data => data.myId === id
? { // <-- updating element into new object reference
...data, // <-- shallow copy previous data element
property: value // <-- update property with new value
}
: data // <-- not updating, pass previous object through
);
}
}
Once you are correctly updating the array elements, then no mutations will occur to anything that may also be referencing the state.
推荐阅读
- autodesk-forge - Autodesk Forge TokenFlex API:针对chargedItemID 的POST 查询BadRequestError
- docker - 有没有办法使用特定版本的 nuget 运行“dotnet nuget”?
- mysql - 如何忽略 group_concat 中的空格
- sql - 如何在此排序表/列表中返回“每个组的顶级成员”?
- google-cloud-platform - 将查询结果移动到另一个位置
- r - R:在等长的子区间中分割区间并保留信息
- c++ - Makefile 在特定系统中失败
- python-3.x - 在 ScikitLearn 的 GroupKFold 中出现错误消息:ValueError: too many values to unpack (expected 2)
- javascript - 在第一次迭代完成后更改计时器间隔
- r - 使用 print.xtableList 时如何删除添加到行名的附加数字,当它们跨表相同时