如何定义与仿制药的react自定义钩的定义?

问题描述 投票:0回答:1

LET考虑了对具有参数触发的参数触发的React自定义挂钩的这些类型定义,而Redux工具包的API呼叫的突变结果:

type UseCallToActionModalRendererParams<T extends (...args: any) => any> = {
    mutationTrigger: () => ReturnType<ReturnType<T>[0]>;
    mutationResult: ReturnType<T>[1];
};

type UseCallToActionModalRendererResult = {
    title: string;
};

type UseCallToActionModalRendererHook<T extends (...args: any) => any> = (
    params: UseCallToActionModalRendererParams<T>,
) => UseCallToActionModalRendererResult;

这个行不通:

export const useCallToActionModalRenderer: UseCallToActionModalRendererHook = ({
    mutationTrigger,
    mutationResult,
}) => {

与以下错误

Generic type 'UseCallToActionModalRendererHook' requires 1 type argument(s).ts(2314)
type UseCallToActionModalRendererHook<T extends (...args: any) => any> = (params: UseCallToActionModalRendererParams<T>) => UseCallToActionModalRendererResult

这有效:

export const useCallToActionModalRenderer = <T extends (...args: any) => any>({
    mutationTrigger,
    mutationResult,
}: UseCallToActionModalRendererParams<T>): UseCallToActionModalRendererResult => {

使用

T
集来调用自定义挂钩,例如,这种类型是Redux-toolkit的突变。
它似乎是相同的方法,具有定义或完整的钩子,或用于输入和输出。
但是为什么第一种方法失败呢?

reactjs typescript react-hooks redux-toolkit
1个回答
0
投票

由于您必须提供类型。您没有在那里提供类型。

从该类型,

useCancelDataMutation

内部的

mutationTrigger

mutationResult
的类型受到限制,但是与不同的特定类型有多种不同的用途。
    

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.