ngrx/store 状态未在调用的 reducer 函数中更新
ngrx/store state not updating in reducer function called
我开始将 ngrx(8.4.0) 添加到现有的 Angular(8.2) 应用程序,但我的一项操作(请参阅下面的操作)在触发时不会改变状态。
auth.actions.ts(部分)
export const loginSuccess = createAction(
'[SIGN IN] Login Success',
props<{ user: any; isNewUser: boolean }>()
loginSuccess
动作由下面的 reducer 函数处理。
auth.reducer.ts(部分)
export interface AuthState {
user: any;
isNewUser: boolean;
}
export const initialState: AuthState = {
user: null,
isNewUser: null
};
const authReducer = createReducer(
initialState,
on(loginSuccess, (state, { user, isNewUser }) => ({
...state,
user: user,
isNewUser: isNewUser
}))
);
export function reducer(state: AuthState | undefined, action: Action) {
return authReducer(state, action);
}
loginSuccess
动作是从名为 loginWithPopUp
的效果中调度的。
loginWithPopUp$ = createEffect(() =>
this.actions$.pipe(
ofType(authActions.loginWithPopUp),
distinctUntilChanged(),
switchMap(action =>
from(this.authService.signUpWithPopUp()).pipe(
map((result: firebase.auth.UserCredential) =>
authActions.loginSuccess({
user: result.user,
isNewUser: result.additionalUserInfo.isNewUser
})
),
tap(() => this.router.navigate(['/notes'])),
catchError(() => of(authActions.loginFail()))
)
)
)
);
即使我的操作被触发并且我在我的操作中看到属性 user
和 isNewUser
,状态也没有更新。
user
和 isNewUser
填充在操作中。
控制台上出现错误。
代码看起来不错,你能验证一下吗:
- reducer 被调用,你可以在里面放一个console.log 或者调试语句
- 确保用户和 isNewUser 已填充到操作中,您可以通过单击开发工具中的操作开关来完成此操作
我也在为同样的问题苦苦挣扎,终于(即使不是完全)弄明白了这个issue的问题所在。
似乎传递完整的 result.user
会产生错误,请尝试传递 UID 或类似的东西。
我开始将 ngrx(8.4.0) 添加到现有的 Angular(8.2) 应用程序,但我的一项操作(请参阅下面的操作)在触发时不会改变状态。
auth.actions.ts(部分)
export const loginSuccess = createAction(
'[SIGN IN] Login Success',
props<{ user: any; isNewUser: boolean }>()
loginSuccess
动作由下面的 reducer 函数处理。
auth.reducer.ts(部分)
export interface AuthState {
user: any;
isNewUser: boolean;
}
export const initialState: AuthState = {
user: null,
isNewUser: null
};
const authReducer = createReducer(
initialState,
on(loginSuccess, (state, { user, isNewUser }) => ({
...state,
user: user,
isNewUser: isNewUser
}))
);
export function reducer(state: AuthState | undefined, action: Action) {
return authReducer(state, action);
}
loginSuccess
动作是从名为 loginWithPopUp
的效果中调度的。
loginWithPopUp$ = createEffect(() =>
this.actions$.pipe(
ofType(authActions.loginWithPopUp),
distinctUntilChanged(),
switchMap(action =>
from(this.authService.signUpWithPopUp()).pipe(
map((result: firebase.auth.UserCredential) =>
authActions.loginSuccess({
user: result.user,
isNewUser: result.additionalUserInfo.isNewUser
})
),
tap(() => this.router.navigate(['/notes'])),
catchError(() => of(authActions.loginFail()))
)
)
)
);
即使我的操作被触发并且我在我的操作中看到属性 user
和 isNewUser
,状态也没有更新。
user
和 isNewUser
填充在操作中。
控制台上出现错误。
代码看起来不错,你能验证一下吗:
- reducer 被调用,你可以在里面放一个console.log 或者调试语句
- 确保用户和 isNewUser 已填充到操作中,您可以通过单击开发工具中的操作开关来完成此操作
我也在为同样的问题苦苦挣扎,终于(即使不是完全)弄明白了这个issue的问题所在。
似乎传递完整的 result.user
会产生错误,请尝试传递 UID 或类似的东西。