我第一次在项目中使用Redux。我有多个reducer和and动作。分派第一个操作时,状态将更改。看起来不错。分派第二个动作后,状态将再次更改,但先前的更改将被删除。我的意思是,当调度“ FETCH_COMPANY_INFORMATIONS”时,companyName会更改,而companyDesc会设置为初始值。然后,调度“ FETCH_INITIAL_MEMBER”,删除companyName,但companyDesc仍然存在,并且成员有效负载也被更改。我怎么了谢谢。
我尝试了多种方法来解决此问题,但仍会继续。我在Redux DevTools上检查了这一点。
memberReducer
const initialState = {
username: '',
companyId: '',
isAdmin: '',
photo: '',
};
export default (state = initialState, action) => {
switch (action.type) {
case FETCH_INITIAL_MEMBER:
return {
...state,
username: action.payload.username,
companyId: action.payload.companyId,
isAdmin: action.payload.isAdmin,
};
default:
return state;
}
};
companyReducer
const initialState = {
companyName: 'companyName',
companyDesc: 'companyDesc',
};
export default (state = initialState, action) => {
switch (action.type) {
case FETCH_COMPANY_INFORMATIONS:
return {
...state,
companyName: action.payload.companyName,
};
default:
return state;
}
};
memberAction
const fetchInıtıalMember = async muuid => {
axios
.get(`/api/member/${muuid}`)
.then(response => {
const username = response.data.mname;
const isAdmin = response.data.misAdmin;
const companyId = response.data.cid;
store.dispatch({
type: FETCH_INITIAL_MEMBER,
payload: {
username,
isAdmin,
companyId,
},
});
})
.catch(error => {});
};
companyAction
const fetchCompanyInformations = () => {
store.dispatch({
type: FETCH_COMPANY_INFORMATIONS,
payload: { companyName: 'dispacthedCompanyName' },
});
};
编辑: 上面的代码是正确的。我的错误是关于导入常量的。这个Redux实施效果很好。我将所有操作类型常量存储在type.js文件中。我在其他文件中错误地导入了此类型常量。更改后,我的问题解决了。