【发布时间】:2020-03-28 04:42:14
【问题描述】:
我想将我的 redux store id 与 action payload id 进行比较,并使用 find 方法更新我的 redux store 状态。当我第三次比较相同的值时,find 方法只工作两次,所以 find 方法给了我这个错误:
TypeError: 无法读取未定义的“查找”属性
const initialState = {
frontendCategory: [],
backendCategory: [],
devOpsCategory: [],
};
const reducer = (state = initialState, action) => {
switch (action.type) {
case actionTypes.ADDED_USER_TO_FRONTEND_CATEGORY:
let updateddata = state.frontendCategory.find(data => data.id === action.payLoad.id);
if (updateddata) {
return [...state.frontendCategory];
}
return {
...state,
frontendCategory: state.frontendCategory.concat(action.payLoad),
};
}
};
export const addedUserToFrontendCategory = (GithubUserId) => {
return {
type: actionTypes.ADDED_USER_TO_FRONTEND_CATEGORY,
payLoad: {
id: GithubUserId,
},
};
};
【问题讨论】:
-
if(updateddata){ return [...state.frontendCategory] }你只是想检查frontendCategory中是否已经存在 id 吗?因为变量名updateddata让我很困惑。 -
因为如果是这样的话,我想你想返回整个状态
if(updateddata) { return state } -
是的,它对我有用!!非常感谢!!!
标签: reactjs redux react-redux state