[英]How to access getState in redux toolkit in typescript?
I have an createAsyncThunk
operation and want to access getState
inside the toolkit.我有一个createAsyncThunk
操作,想访问工具包中的getState
。 I am not sure about how to cast getState
type in typescript:我不确定如何在 typescript 中转换getState
类型:
export const getInitialDatapoints = createAsyncThunk(
'userSlice/getInitDatapoints',
async (args, { getState as AppState }) => {
const geoData = await getLocationData();
const googleData = getState().userSlice;
const response = await updateUserData(1);
return response.data;
}
);
where AppStore
is: AppStore
在哪里:
export type AppState = ReturnType<typeof store.getState>;
I get this error:我收到此错误:
Property 'AppState' does not exist on type 'GetThunkAPI<{}>'
Can anyone help in solving this issue?任何人都可以帮助解决这个问题吗?
You can't cast types while destructuring objects.解构对象时不能转换类型。 Also, you need to call getState
before you can cast it to AppState
.此外,您需要先调用getState
才能将其转换为AppState
。
You can do this instead:你可以这样做:
const getInitialDatapoints = createAsyncThunk(
"userSlice/getInitDatapoints",
// you could also just stick to { getState }; call and
// cast it in the line below.
async (args, api) => {
const appState = api.getState() as AppState
const geoData = await getLocationData();
const googleData = appState.userSlice;
const response = await updateUserData(1);
return response.data;
}
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.