【发布时间】:2021-06-25 15:05:56
【问题描述】:
为什么我的过滤功能不起作用?
ShipperSlice.js
import { createSlice } from "@reduxjs/toolkit";
import { current } from '@reduxjs/toolkit';
const createProductShipper = createSlice({
name: "createProductShipper",
initialState: {
shippers: []
},
reducers: {
AddProductShipper: (state, action) => {
const shipperExists = state.shippers.find(el => el?.id === action.payload.id);
if(typeof shipperExists !== 'undefined') {
return state.shippers.filter(el => el.id !== action.payload.id);
} else {
state.shippers.push(action.payload)
}
console.log(current(state.shippers));
},
}
});
export const { AddProductShipper, RemoveProductShipper } = createProductShipper.actions;
export default createProductShipper.reducer;
这是推送输出
Array [
Object {
"id": 1,
"shipper": "SHIPPER",
},
]
如果托运人存在,那么我会收到此错误: ....................
TypeError: undefined is not an object (evaluating 'state.shippers.find')
为什么我的托运人对象被删除了?
The State:
Object {
"shippers": Array [],
}
First Click:
Object {
"shippers": Array [
Object {
"id": 1,
"shipper": "DHL",
},
],
}
Second Click:
Array []
.................................................. ......
【问题讨论】:
标签: react-native redux expo redux-toolkit