【发布时间】:2022-12-22 02:48:27
【问题描述】:
所以我有这个函数,其中“useCustom”是我在 Typescript 上的自定义 Hook,它基本上使用 json 文件替换字符串。
import { Stringable } from './types';
export declare const useCustom: () => (key: string, replace?: Stringable[] | undefined) => string;
export function* calling(action: any) {
const custom = useCustom();
try {
yield call(status, custom('Template applied.'), StatusType.success);
} catch (e) {
yield put(getFail(e));
}
}
问题是我得到这个编译错误:
React Hook“useCustom”在既不是 React 函数组件也不是自定义 React Hook 函数的函数“调用”中被调用。 React 组件名称必须以大写字母开头。 React Hook 名称必须以单词“use”react-hooks/rules-of-hooks 开头。
使用函数式编程编译成功但它不起作用(字符串永远不会改变):
function Custom(label: string) {
const custom = useCustom();
let customized = custom(label);
return customized !== '' ? customized : label;
}
export function* calling(action: any) {
try {
yield call(status, Custom('Template applied.'), StatusType.success);
} catch (e) {
yield put(getFail(e));
}
}
我不熟悉这种函数:"function*" 也不熟悉 yield。我尝试了很多不同的东西,但完全迷失了使我的自定义挂钩工作的过程。
有任何想法吗?
谢谢
【问题讨论】:
-
您是否尝试过收听错误消息并改用
useCalling?
标签: typescript function react-hooks yield