我的用户结构是:
用户={电子邮件:"电子邮件",抽认卡:[]}
我想把数据添加到用户的闪卡阵列中(使用redux(
我的用户还原
import { UserActionTypes } from './user.types';
const INITIAL_STATE = {
currentUser: null,
};
// GETS STATES OBJ AND RECIVES AN ACTION
const userReducer = (state = INITIAL_STATE, action) => {
switch (action.type) {
case UserActionTypes.SET_CURRENT_USER:
return {
...state,
currentUser: action.payload,
};
case UserActionTypes.ADD_FLASHCARD:
return {
...state,
currentUser: action.payload,
};
default:
return state;
}
};
export default userReducer;
用户操作
export const addFlashCard = user => ({
type: UserActionTypes.ADD_FLASHCARD,
payload: user.flashcards,
});
当我这样做的时候,我的有效载荷是未定义的。
你能给我一些提示吗?
您当前正在使用redux操作中的user.flashcards
值覆盖currentUser
。要添加新的抽认卡,减速器的ADD_FLASHCARD
分支应该更像这样:
case UserActionTypes.ADD_FLASHCARD:
return {
...state,
currentUser: {
...state.currentUser,
flashcards: [
...state.currentUser.flashcards,
...action.payload
]
}
};