Erw*_*han 2 reactjs redux-thunk redux-toolkit
所以我想访问我的状态的当前状态以state.formLogin存储在变量中userCredentials我如何才能访问 createAsyncThunk 上的状态?
export const loginAsync = createAsyncThunk(
"user/login",
async () => {
try {
const { content } = await login(userCredentials);
localStorageHelpers.setUserToken(content.token);
localStorageHelpers.setUserId(content.userId);
return content.userId;
} catch (err) {
console.log(err)
}
}
);
Run Code Online (Sandbox Code Playgroud)
您可以通过调用 来访问整个状态thunkAPI.getState()。参见有效负载创建器
export const loginAsync = createAsyncThunk(
"user/login",
async (_, thunkAPI) => {
try {
// const state = thunkAPI.getState();
const { content } = await login(userCredentials);
localStorageHelpers.setUserToken(content.token);
localStorageHelpers.setUserId(content.userId);
return content.userId;
} catch (err) {
console.log(err)
}
}
);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2458 次 |
| 最近记录: |