如何在 createAsyncThunk 上访问 Redux Toolkit 的当前状态

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)

sli*_*wp2 5

您可以通过调用 来访问整个状态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)