简体   繁体   English

为什么我不能让 Typescript yield 与自定义挂钩一起使用?

[英]Why can't I get Typescript yield to work with custom hooks?

So I have this function where "useCustom" is my custom Hook on Typescript which basically replaces a string using a json file.所以我有这个函数,其中“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));
  }
}

The problem is I get this compile error:问题是我得到这个编译错误:

React Hook "useCustom" is called in function "calling" that is neither a React function component nor a custom React Hook function. React Hook“useCustom”在既不是 React 函数组件也不是自定义 React Hook 函数的函数“调用”中被调用。 React component names must start with an uppercase letter. React 组件名称必须以大写字母开头。 React Hook names must start with the word "use" react-hooks/rules-of-hooks. React Hook 名称必须以单词“use”react-hooks/rules-of-hooks 开头。

Using functional programming compiles successfully but it doesn't work (the string never changes):使用函数式编程编译成功但它不起作用(字符串永远不会改变):

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));
      }
    }

I'm not familiar with this kind of functions: "function*" neither with yield.我不熟悉这种函数:“function*”也不熟悉 yield。 I've tried many different things and got totally lost on the process to make my custom hook to work.我尝试了很多不同的东西,但完全迷失了让我的自定义挂钩工作的过程。

Any ideas?有任何想法吗?

Thanks谢谢

Your component name should start with capital letter.您的组件名称应以大写字母开头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM