我正在尝试学习 Immunity Helper,因为我认为它会帮助我使用 reducer ,但我不知道如何更改数组的所有对象中的属性。
请帮忙,我被困了这么久
https://github.com/kolodny/immutability-helper https://facebook.github.io/react/docs/update.html
我的初始状态
outputList: [
{ propertyIWantToChange: 'some value1' },
{ propertyIWantToChange: 'some value2' },
{ propertyIWantToChange: 'some value3' }, etc
]
reducer 不工作。我觉得我的语法有点不对劲,但我不知道是哪一部分。
case types.SOME_TYPE: {
return update(state, {
outputList: {
propertyIWantToChange: {$set: action.value}
}
});
最佳答案
您必须迭代该数组。如果该列表接受 map
那么类似的东西应该可以工作,我猜:
case types.SOME_TYPE: {
const lst = state.outputList.map((el) => {
return update(el, { propertyIwantToChange: {$set: action.value}}
});
return update(state, { outputList: {$set: lst}})
}
关于javascript - 使用 Redux Reducer 的 Immunity Helper 迭代数组并更改特定属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42843056/