redux 如何在react readux中推送第N个数组

dzjeubhm  于 2023-06-06  发布在  React
关注(0)|答案(1)|浏览(91)

我想在react redux中推入第N个数组项。但它不推也不给予任何错误。
在函数中我尝试喜欢这个

const id = action.payload.container_id;

console.log("id", id);

// state.containers
let key = 0;
state.containers = state.containers.map((cont) => {
  console.log("key", key);
  if (cont.info.id === id) {
    console.log("found container atrr", state.containers[key].info.title);
    state.containers[key].info.attributes.push({
      id: action.payload.container_attr_id,
      value: action.payload.data.value,
      name: action.payload.data.name,
    });
  }
  key = key + 1;
});
tjjdgumg

tjjdgumg1#

我发现了我的错误。需要返回

const id = action.payload.container_id;
  console.log("id", id);
  console.log("action payload", action.payload);
  // state.containers
    let key = 0
    state.containers = state.containers.map((cont) => {
      console.log("key", key);
      if (cont.info.id === id) {
        console.log("found container atrr", state.containers[key].info.title);
        
        state.containers[key].info.attributes.push({
          id: action.payload.container_attr_id,
          value: action.payload.value,
          name: action.payload.name,
        });
      }
      key = key + 1
      **return cont**
    });

相关问题