简体   繁体   English

ReactJS useCallback不是React函数吗?

[英]ReactJS useCallback not a function of react?

I have a functional Component which is using the hook useCallback. 我有一个正在使用钩useCallback的功能组件。 For the last few days everything was just fine. 在过去的几天里,一切都很好。 All worked as it should. 一切都按预期进行。 Today I start up the app and I have this error: 今天,我启动了该应用程序,但出现此错误:

React Hook "useCallback" is called in function "loginPage" which is neither a React function component or a custom React Hook function

This makes no sense because it has been fine. 因为没有问题,所以这没有任何意义。 For debugging I simply erased all code on the page except that one coed and even just put a useCallback template in its place, but still the same. 为了进行调试,我只删除了页面上的所有代码,只剩下一个代码,甚至只是将useCallback模板放到了它的位置,但是仍然一样。 It is as if it has been removed from react entirely. 好像它已完全从反应中删除。

I checked my react version and find 16.8.6 also in react-dom. 我检查了我的React版本,并在react-dom中也找到了16.8.6。

Does anyone have any ideas? 有人有什么想法吗?

import React, {Fragment,useCallback } from 'react';
import { Redirect} from 'react-router-dom';
import {useDropzone} from 'react-dropzone';

const loginPage = (props) => {

    const callbackFunction = useCallback(() => {
        console.log("callback called");
        // Do something with callbackCount ...
        return true;
    }, []);

  }

I see three problems why the error occurred. 我看到了为什么会出现错误的三个问题。

which is neither a React function component or a custom React Hook function 它既不是React函数组件也不是自定义的React Hook函数

  1. loginPage is not a component. loginPage不是组件。 useCallback is a hook and it needs to be called inside a function component (not a class component) or in another hook (a function whose name starts with "use"). useCallback是一个钩子,需要在函数组件(而不是类组件)内部或另一个钩子(名称以“ use”开头的函数)中调用。 Check out the rules of hooks . 检查钩子规则
  2. And also the component name should start with a capital letter, so it should be LoginPage for it to be a valid React component. 组件名称也应该以大写字母开头,因此它应该是LoginPage才能成为有效的React组件。
  3. You are not returning anything from your component. 您没有从组件返回任何东西。

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

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