程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何键入异步 thunk 的调度?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决如何键入异步 thunk 的调度??

开发过程中遇到如何键入异步 thunk 的调度?的问题如何解决?下面主要结合日常开发的经验,给出你关于如何键入异步 thunk 的调度?的解决方法建议,希望对你解决如何键入异步 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 */)

onCallThunkhook.ts 的类型应该是什么?

解决方法

您最好的选择是:您希望使用的部分,例如

type HookProperties = {
  onCallThunk: () => void // for example if you don't care about the result being used
};

或者,简单地说:将 : HookProperties 注释放在一边,然后保留它。 TypeScript 比你自己编写它更了解返回类型,手动写下来你一无所获——除非在你更改某些内容时进行更多维护。

大佬总结

以上是大佬教程为你收集整理的如何键入异步 thunk 的调度?全部内容,希望文章能够帮你解决如何键入异步 thunk 的调度?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:thunk