大佬教程收集整理的这篇文章主要介绍了如何键入异步 thunk 的调度?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我目前正在使用 reduxJs/toolkit,但在输入钩子以返回异步 thunk 的调度时遇到问题。这是我遵循 https://redux-toolkit.js.org/usage/usage-with-typescript(不是全功能代码)的设置:
// thunk.ts
export const callThunk: AsyncThunk<
ResponseBody,voID,{state: RootStatE}
> = createAsyncThunk(
'someAsyncThunkAction',async (_data,{rejectWithvalue}) => {
const response = await someAPICall();
if (!response.ok) {
return rejectWithValue(`Put Failed: ${response.status}`);
} else {
return await response.Json();
}
);
// store.ts
export const store = configureStore({reducer: rootReducer});
export type Appdispatch = typeof store.dispatch;
export const useAppdispatch = () => usedispatch<Appdispatch>();
// hook.ts
import {CallThunk} from './thunk'
import {useAppdispatch} from './store'
type HookPropertIEs = {
onCallThunk: () => // <--- this is the type that I haven't figured out
};
export const useHook = (): HookPropertIEs => {
const appdispatch = useAppdispatch();
return {
onCallThunk: () => appdispatch(callThunk())
};
};
希望我可以在我的组件中调用这个钩子,如下所示:
import {useHook} from './hook'
const {onCallThunk} = useHook();
onCallThunk().then(/* do something here */)
onCallThunk
中 hook.ts
的类型应该是什么?
您最好的选择是:您希望使用的部分,例如
type HookProperties = {
onCallThunk: () => void // for example if you don't care about the result being used
};
或者,简单地说:将 : HookProperties
注释放在一边,然后保留它。 TypeScript 比你自己编写它更了解返回类型,手动写下来你一无所获——除非在你更改某些内容时进行更多维护。
以上是大佬教程为你收集整理的如何键入异步 thunk 的调度?全部内容,希望文章能够帮你解决如何键入异步 thunk 的调度?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。