我想执行该操作,因此将调度发送到错误的行。但是由于这个错误,我很挣扎。
我写了很多变量的代码,这是错误的方法吗?
//reducer
export const signUpEmail = email => ({
type: SIGNUP_EMAIL,
email,
});
export const signUpInput = ({name, nickname, id, password}) => ({
type: SIGNUP_INPUT,
data: {
name: name,
nickname: nickname,
id: id,
password: password,
},
});
export const signUpImg = img => ({
type: SIGNUP_IMG,
img,
});
const signUpState = {
email: '',
data: {
name: '',
nickname: '',
id: '',
password: '',
},
img: '',
};
export const signUpReducer = (state = signUpState, action) => {
switch (action.type) {
case SIGNUP_EMAIL:
return ({ ...state, email: action.email });
case SIGNUP_INPUT:
return ({
...state,
data: {
name: action.name,
nickname: action.nickname,
id: action.id,
password: action.password
},
});
case SIGNUP_IMG:
return ({
...state,
img: action.img,
});
default: return state;
}
};
这是减速器
//selector
const {userEmail, name, nickname, id, password, userImg } = useSelector(state => ({
userEmail: state.email,
userName: state.name,
userNickname: state.nickname,
userId: state.id,
userPassword: state.password,
userImg: state.img
}));
这是商店的状态连接
//dispatch
const userData = ({userName, userNickname, userId, userPassword}) => {
dispatch(signUpInput({userName, userNickname, userId, userPassword}));
}
问题出在哪里?
答案 0 :(得分:0)
确保正确访问对象的子对象:state.nickname
应该是state.data.nickname
,action.nickname
应该是action.data.nickname
,等等。
另外,请尝试重构减速器并将其根据状态的当前形状分为不同的部分。例如,state.data
,state.email
和state.img
可能希望拥有自己的减速器,您可以使用combineReducers
将它们组合为单个减速器。现在有点混乱。