在React/Redux reducer中,如何以不可变的方式更新嵌套数组中的字符串?

Zen*_*fin 5 javascript arrays ecmascript-6 reactjs redux

我的商店看起来像这样,

{
  name: "john",
  foo: {},
    arr: [
      {
        id:101,
        desc:'comment'
      },
      { 
        id:101,
        desc:'comment2'
      }
    ]
}
Run Code Online (Sandbox Code Playgroud)

我的textarea看起来像这样

<textarea
  id={arr.id} //"101"
  name={`tesc:`}
  value={this.props.store.desc}
  onChange={this.props.onChng}
/>
Run Code Online (Sandbox Code Playgroud)

我的行动是

export const onChng = (desc) => ({
  type: Constants.SET_DESC,
  payload: {
    desc
  }
});
Run Code Online (Sandbox Code Playgroud)

我的减速机

case Constants.SET_DESC:
  return update(state, {
    store: {
      streams: {
        desc: { $set: action.payload.desc }
      } 
    }
});
Run Code Online (Sandbox Code Playgroud)

它只有在arry是一个对象时才有效,我不得不对数组流进行更改,我很困惑如何更新到数组,以及如何从商店中获取正确的值.

nol*_*awi 0

要更新数组,我将使用不变性助手并对您的减速器执行类似的操作

 let store = {"state" : {
"data": [{
    "subset": [{
        "id": 1
    }, {
        "id": 2
    }]
}, {
    "subset": [{
        "id": 10
    }, {
        "id": 11
    }, {
        "id": 12
    }]
}]
}}

case Constants.SET_DESC:
  return update(store, {
  "state" : {
  "data": {
      [action.indexToUpdate]: {
        "subset": {
             $set: action.payload.desc
        }
      }
  }
 }
  })
  });
Run Code Online (Sandbox Code Playgroud)