I have two async thunks created by createAsyncThunk()
function of redux-toolkit
.
E.g.
const getOrder = createAsyncThunk('a/getOrder', async (_, thunkAPI) => {
// `userId` from API.getUser()
// I tried to get the `userId` from redux store, but got `undefined`.
const user = thunkAPI.getState().user;
const userId = user.userId;
const response = await APIs.getOrder(userId);
return response.result;
});
const getUser = createAsyncThunk('a/getUser', async () => {
const response = await APIs.getUser();
return response.result;
});
In component, I dispatched these two async thunks concurrently.
useEffect(() => {
dispatch(getUser());
dispatch(getOrder());
}, []);
getOrder
thunk depend on the result of getUser()
thunk. What's the correct way to get the userId
parameter? Can I get the userId
from the redux store user
slice state?