简体   繁体   English

如何在自定义钩子中使用 useCallback

[英]How to use useCallback inside custom hook

got little issue here.这里没有什么问题。 I'm exporting a custom hook, which uses other hooks, but I get "Unhandled Rejection (Error): Invalid hook call..." Error.我正在导出一个使用其他挂钩的自定义挂钩,但出现“未处理的拒绝(错误):无效的挂钩调用...”错误。 Looks like I'm not doing something wrong... I'm using hook inside hook, so why is this error being thrown?看起来我没有做错什么......我在钩子中使用钩子,那么为什么会抛出这个错误?

const customHook = async () => {
const { values } = useValues()

const testCallback = useCallback(async () => {
    const value = values

    return value
}, [value])

 return testCallback 
}

export default customHook 

I think the issue can be resolved by just changing the case of your component's name, try renaming it to "CustomHook".我认为只需更改组件名称的大小写即可解决此问题,尝试将其重命名为“CustomHook”。 There's a rule with react hooks that you can not call use hook inside a function or component whose name is in Camel Case.有一个 react hooks 规则,你不能在 function 或名称为 Camel Case 的组件中调用 use hook。

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

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