反应本机 redux 工具包过滤器不起作用
react native redux toolkit filter not working
为什么我的过滤功能不起作用?
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 []
................................................ .........
改变return state.shippers.filter(el => el.id !== action.payload.id);
收件人:state.shippers = state.shippers.filter(el => el.id !== action.payload.id);
为什么我的过滤功能不起作用?
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 []
................................................ .........
改变return state.shippers.filter(el => el.id !== action.payload.id);
收件人:state.shippers = state.shippers.filter(el => el.id !== action.payload.id);