来自一个redux工具箱的多个操作thunk



工具包,我正在努力了解createAsyncThunk是如何适应这一切的。我想打电话到我的服务器,在一个电话中获取会话信息和用户信息。然而,从我目前在createAsyncThunk上看到的情况来看,它只使用了一个状态切片?有人能详细说明一下我如何同时访问会话和用户吗?下面是一个示例。

const payload = await myApi.login(loginInfo)
dispatch(loginSession(payload.session)
dispatch(loginUser(payload.user)
export const sessionSlice = createSlice({
name:'session',
initialState,
reducers: {
loginSession: (state,action:PayloadAction<schema.Session>) => {

state = action.payload
},
},
})
export const userSlice = createSlice({
name: 'user',
initialState,
reducers: {
loginUser:(state,action:PayloadAction<schema.User>) => {
if (state !== null) {
throw new Error('a user is already logged in')
}
state = action.payload
},
}
})

const payload = await myApi.login(loginInfo)
dispatch(loginFulfilled({ 
session: payload.session,
user: payload.user,
})
export const sessionSlice = createSlice({
name:'session',
initialState,
reducers: {
loginFulfilled: (state,action:PayloadAction<{session: schema.Session}>) => {

state = action.payload.session
},
},
})
export const userSlice = createSlice({
name: 'user',
initialState,
reducers: {
loginFulfilled:(state,action:PayloadAction<{user: schema.User>}) => {
if (state !== null) {
throw new Error('a user is already logged in')
}
state = action.payload.user
},
}
})

相关内容

  • 没有找到相关文章

最新更新