首先,我知道(或者我想我已经读过)你永远不应该从减速器中触发动作。在我的情况下,我redux-oidc
用于处理针对我的应用程序的身份验证。用户登录后,redux-oidc
触发一个操作并在切片redux-oidc/USER_FOUND
中设置用户的个人资料。state.oidc.user
登录后,我需要从我的数据库中查找不在 OIDC 响应中的有关用户的其他信息。目前,我正在按fetchUserPrefs
预期redux-oidc.CallbackComponent.successCallback
工作。
我的问题是当用户有一个活跃的会话并打开一个新的浏览器,或者手动刷新页面并再次初始化应用程序时,回调没有被命中,所以额外的用户水合作用不会发生。似乎我想要做的是添加一个extraReducer
监听redux-oidc/USER_FOUND
动作并触发 thunk 的操作,但这会从减速器触发一个动作。
有一个更好的方法吗?
import {createAsyncThunk, createSlice} from '@reduxjs/toolkit';
import { RootState } from '../../app/store';
import {User} from "oidc-client";
export const fetchUserPrefs = createAsyncThunk('user/fetchUserPrefs', async (user: User, thunkAPI) => {
// the call out to grab user prefs
// this works as expected when dispatched from the CallbackComponent.successCallback
return user;
})
function hydrateUserState(state: any, action: any) {
// set all the state values from the action.payload
// this works as expected
}
export interface UserState {
loginId: string;
firstName: string;
lastName: string;
email: string;
photoUrl: string;
}
const initialState: UserState = {
loginId: '',
firstName: '',
lastName: '',
email: '',
photoUrl: '',
};
export const userSlice = createSlice({
name: 'user',
initialState,
reducers: {
},
extraReducers: (builder) => {
builder
.addCase('redux-oidc/USER_FOUND', fetchUserPrefs) // I want to do this, or something like it
.addCase(fetchUserPrefs.fulfilled, hydrateUserState)
.addDefaultCase((state, action) => {})
}
});
export const selectUser = (state: RootState) => state.user;
export default userSlice.reducer;