getState().属性 return 在异步函数中未定义
getState().property return undefined inside an async function
我正在使用 react-thunk
和 redux
来实现登录功能,但是当我使用 getState()
访问状态时, 属性 returns未定义。
SystemState
将在 LoginSuccessed
时设置 IsLogined
和 user
,稍后我将使用它作为访问令牌。但是当我尝试实现注销功能时,getState().Property
returns undefined
function logout() : ThunkAction<void, SystemState, null, Action<string>> {
return async (dispatch, getState) => {
const { user, isLogined } = getState();
console.log(`user: ${user} isLogined:${isLogined}` )
// Console writes user: undefined isLogined: undefined
if (!isLogined) {
dispatch(actions.LogoutFailed("No login Session Found"));
} else if (user) {
const result = await services.Logout(user.UserId);
if (result.errorMessage) {
dispatch(actions.LogoutFailed(result.errorMessage));
} else {
dispatch(actions.Logout());
}
}
}
}
不应该在异步函数中调用 getState()
吗?
--- 编辑 ---
SystemSate 的减速器
const initState = {
isLogined: false
}
function SystemStateReducer(state = initState, action) {
switch(action.type) {
case types.LOGIN_SUCCESS:
return {
...state,
isLogined: true,
user: action.user,
errorMessage: undefined
}
case types.LOGIN_FAILED:
return {
...state,
isLogined: false,
user: undefined,
errorMessage: action.errorMessage
}
case types.LOGOUT_SUCCESS:
return {
...state,
isLogined: false,
user: undefined,
errorMessage: undefined
}
case types.LOGOUT_FAILED:
return {
...state,
isLogined: false,
user: undefined,
errorMessage: action.errorMessage
}
default:
return state;
}
}
CombineReducer
const rootReducer = combineReducers({
systemState
});
问题是您误解了状态结构。
您已将根减速器定义为:
const rootReducer = combineReducers({
systemState
});
systemState
reducer 的初始状态为:
const initState = {
isLogined: false
}
这两件事定义了 getState()
返回的结构,实际上是:
{
systemState : {
isLogined : true
}
}
请注意,这意味着您需要 state.systemState.isLogined
,而不是 state.isLogined
。
因此,在您的情况下,您可能想要的是:
const state = getState();
const {isLogined} = state.systemState;
我正在使用 react-thunk
和 redux
来实现登录功能,但是当我使用 getState()
访问状态时, 属性 returns未定义。
SystemState
将在 LoginSuccessed
时设置 IsLogined
和 user
,稍后我将使用它作为访问令牌。但是当我尝试实现注销功能时,getState().Property
returns undefined
function logout() : ThunkAction<void, SystemState, null, Action<string>> {
return async (dispatch, getState) => {
const { user, isLogined } = getState();
console.log(`user: ${user} isLogined:${isLogined}` )
// Console writes user: undefined isLogined: undefined
if (!isLogined) {
dispatch(actions.LogoutFailed("No login Session Found"));
} else if (user) {
const result = await services.Logout(user.UserId);
if (result.errorMessage) {
dispatch(actions.LogoutFailed(result.errorMessage));
} else {
dispatch(actions.Logout());
}
}
}
}
不应该在异步函数中调用 getState()
吗?
--- 编辑 ---
SystemSate 的减速器
const initState = {
isLogined: false
}
function SystemStateReducer(state = initState, action) {
switch(action.type) {
case types.LOGIN_SUCCESS:
return {
...state,
isLogined: true,
user: action.user,
errorMessage: undefined
}
case types.LOGIN_FAILED:
return {
...state,
isLogined: false,
user: undefined,
errorMessage: action.errorMessage
}
case types.LOGOUT_SUCCESS:
return {
...state,
isLogined: false,
user: undefined,
errorMessage: undefined
}
case types.LOGOUT_FAILED:
return {
...state,
isLogined: false,
user: undefined,
errorMessage: action.errorMessage
}
default:
return state;
}
}
CombineReducer
const rootReducer = combineReducers({
systemState
});
问题是您误解了状态结构。
您已将根减速器定义为:
const rootReducer = combineReducers({
systemState
});
systemState
reducer 的初始状态为:
const initState = {
isLogined: false
}
这两件事定义了 getState()
返回的结构,实际上是:
{
systemState : {
isLogined : true
}
}
请注意,这意味着您需要 state.systemState.isLogined
,而不是 state.isLogined
。
因此,在您的情况下,您可能想要的是:
const state = getState();
const {isLogined} = state.systemState;