我有一个authReducer
和用户状态。
我也有一个generalReducer
,状态与错误通知消息有关,并且正在加载true / false。
如何在将登录名或其他任务加载到单独的化简器中时,从一个化简器修改或创建与另一个化简器相关的全局loading
状态变量?
generalReducer.js :
let defaultState = {
loading: false,
error: null,
notification: false,
};
export default function generalReducer(state = defaultState, action){
switch(action.type){
case loading:
return {
...state,
loading: action.payload.isLoading,
};
default:
return state;
}
authReducer.js
let defaultState = {
user: {
displayName: null,
email: null,
photoUrl: null,
isAnonymous: null,
phone: null,
}
};
export default function authReducer(state = defaultState, action){
switch(action.type){
case LOGIN_BEGIN:
case LOGOUT_BEGIN:
return {
...state,
};
case LOGIN_FAILURE:
case LOGOUT_FAILURE:
return {
...state,
};
case LOGIN_SUCCESS:
return {
...state,
user: action.payload.user
};
default:
return state;
}
}
在authReducer.js
中,我希望能够在loading
中设置error
或generalReducer
状态,以与所需的错误或加载状态相对应。怎么样?
答案 0 :(得分:0)
您需要从authActions导入authActionTypes并在generalReducer.js中更新reducer
generalReducer.js:
import {
LOGIN_BEGIN,
LOGOUT_BEGIN,
LOGIN_FAILURE,
LOGOUT_FAILURE,
LOGIN_SUCCESS,
} from './authActions;
let defaultState = {
loading: false,
error: null,
notification: false,
};
export default function generalReducer(state = defaultState, action){
switch(action.type){
case loading:
return {
...state,
loading: action.payload.isLoading,
};
case LOGIN_BEGIN:
case LOGOUT_BEGIN: {
return {
...state,
loading: true,
error: null,
}
}
case LOGIN_FAILURE:
case LOGOUT_FAILURE: {
return {
...state,
loading: false,
error: 'Set Your Error Here',
}
}
case LOGIN_SUCCESS: {
return {
...state,
loading: false,
error: null,
}
}
default:
return state;
}
希望这会有所帮助。