next.js 如何将对象添加到Redux Reducers中?

guz6ccqo  于 2023-01-20  发布在  其他
关注(0)|答案(1)|浏览(151)

我正在使用Redux与Next.js,我试图添加对象数组到它。在我的应用程序中,我有一个具有多个输入的表单。为了创建这个多个输入,我创建了一个10的输入表单。

{homeAmeneties.map((home) => {
    <div className="h-8 flex justify-end  rounded-md pr-10">
                    <input
                      type="number"
                      onInput={(event) =>
                        (event.target.value = event.target.value.slice(
                          0,
                          event.target.maxLength
                        ))
                      }
                      maxLength="3"
                      onChange={(e) => updateVal(e, home.name)}
                      className="border-[1px] w-1/2  border-black rounded-md h-full focus:outline-none px-2"
                    />
                  </div>
})}

每一个来自输入的值被添加到一个数组.但是我想把它添加到redux数组.在我的redux reducer中我已经创建了

import { createSlice } from "@reduxjs/toolkit";

const initialState = {
  arr: []
}

export const arrSlice = createSlice({
  name: "arrS",
  initialState,
  reducers: {
    setArr: (state, action) => {
      state.arr = [...state.arr, action.payload];
    }
  }
});

//...rest here

当我运行这个函数时,它抛出了TypeError: action.payload is not iterable,我试着通过传递state.arr = action.payload;来添加它,它没有将输入存储为对象数组,只是简单的对象。
我如何添加和制作tweek就像我可以在简单的useState中做一样?
下面的代码是我对常规useState()所做的

const [amen, setAmen] = useState([]);

const updateVal = (e, type) => {
    setAmen((prevAmen) => {
      const newAmen = [...prevAmen]; // create a copy of the current state
      const index = newAmen.findIndex((item) => item.name === type); // find the index of the object with the matching name
      if (e.target.value === "") {
        if (index !== -1) {
          newAmen.splice(index, 1);
        }
      } else {
        if (index !== -1) {
          newAmen[index] = { name: type, val: e.target.value }; // update the object at that index
        } else {
          newAmen.push({ name: type, val: e.target.value }); // add a new object to the array
        }
      }
      return newAmen;
    });
    dispatch(setRooms(...amen));
    console.log(rooms);
  };
nnsrf1az

nnsrf1az1#

如果有人想要解决方案,我找到了这个问题的答案。在此之前,让我解释一下它的作用。
我有一个来自输入字段的对象数组。当这些字段的值改变时,它会触发Redux分派。如果数组存在,它会更新该数组,如果不存在,它会创建新数组。
我的数据如下所示

[
  {name: "name1" , val: 27},
  {name: "name2" , val: 26},
  {name: "name3" , val: 25},
  {name: "name4" , val: 24},
  {name: "name5" , val: 23},
  {name: "name6" , val: 22},
]

并且当输入onChange()触发时,它将数据分派到redux。onChange={(e) => handleChange(name, e.target.value)}

const handleChange = (name, val) =>{
  const obj = { name: name, val: val };
  dispatch(setUser(obj))
}

在减速器中

setUser: (state, action) => {
      const newState = [...state.users];
      const type = action.payload;

      const index = newState.findIndex((item) => item.name === type.name);

      if (type.val === "") {
        if (index !== -1) {
          newState.splice(index, 1);
        }
      } else {
        if (index !== -1) {
          newState[index] = { name: type.name, val: type.val };
        } else {
          newState.push({ name: type.name, val: type.val });
        }
      }

      state.users = [...newState];
      console.log(state.users);
    },

希望这能帮上忙。

相关问题