简体   繁体   English

尽管代码运行,但箭头功能组件内的钩子调用无效

[英]Invalid hook call inside arrow functional component although the code runs

I'm learning react and am trying to get a simple pop-up to show whenever the Show Toast button is clicked using Toast .我正在学习 react 并尝试在使用Toast单击Show Toast按钮时Show Toast一个简单的弹出窗口。 However, I'm receiving this error:但是,我收到此错误:

Error in /turbo_modules/react-dom@16.13.1/cjs/react-dom.development.js (12408:27)
Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app

I'm assuming it's something simple although I'm not used to hooks.我假设它很简单,虽然我不习惯挂钩。 The error states that hooks can only be called inside the body of a functional component, so:该错误指出只能在功能组件的主体内部调用钩子,因此:

  1. Where is technically the body of a functional component?从技术上讲,功能组件的主体在哪里? I assume where I've put this comment: //body of functional component here我假设我把这条评论放在哪里: //body of functional component here
  2. Why is this error also appearing in the JS console: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in the componentWillUnmount method为什么JS控制台也出现这个错误: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in the componentWillUnmount method Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in the componentWillUnmount method
  3. How can I get the pop-up to show?我怎样才能让弹出窗口显示? The console.log('click') shows in the console so I assume it's the succeeding toast code that's incorrect console.log('click')显示在控制台中,所以我认为这是不正确的后续toast代码

Here is a stackblitz demo这是一个stackblitz演示

And here is the code:这是代码:

import React from "react";
import { useForm } from "react-hook-form";
import { useToast } from "@chakra-ui/core";

const App = () => {
  const { register, handleSubmit, watch, errors } = useForm();
  //const toast = useToast();
  const onSubmit = data => {
      console.log('data', data);
    };

  // body of functional component here?

  const toastTest = () => 
    {
      const toast = useToast();
      return (
      <button
        onClick={() =>
          {
            console.log('click');
            toast({
              title: "Account created.",
              description: "We've created your account for you.",
              status: "success",
              duration: 9000,
              isClosable: true,
            })
          }
        }
      >
        Show Toast
      </button>
    )
  };

  console.log(watch("example"));

  return (
    <>

    {toastTest()}

    <form onSubmit={handleSubmit(onSubmit)}>
      <input name="example" defaultValue="test" ref={register} />
      
      <input name="exampleRequired" ref={register({ required: true })} />
      {errors.exampleRequired && <span>This field is required</span>}
      
      <input type="submit" />
    </form>
    </>
  );
}

export default App

I've seen similar other posts from other users but cannot get this to work.我看过来自其他用户的类似的其他帖子,但无法让它发挥作用。 Any beginner advice to hooks would be appreciated, thank you.任何对钩子的初学者建议将不胜感激,谢谢。

Technically you are violating the Rules of Hooks .从技术上讲,您违反了Hooks 规则 You should move out useToast from the function's body called toastTest into the root of your function component.您应该将useToast从名为toastTest的函数主体toastTest到函数组件的根中。

See my suggested solution below:请参阅下面我建议的解决方案:

const App = () => {
  const toast = useToast(); // useToast moved here from the function
  const { register, handleSubmit, watch, errors } = useForm();

  const onSubmit = data => {
      console.log('data', data);
  }

  const toastTest = () => 
     // please notice useToast is removed from here
     // ... rest of the function's body
  }

  return <> { /* your return */ }</>
}

暂无
暂无

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

相关问题 Refs 的功能组件中的 Hook 调用无效 - Invalid Hook Call In Functional Component for Refs 收到此错误“无效的挂钩调用。 钩子只能在 function 组件的主体内部调用。” 在反应功能组件内? - Getting this error “Invalid hook call. Hooks can only be called inside of the body of a function component.” inside a react functional component? React - Invalid Hook Call:将 App 组件转换为功能组件,仍然得到 Invalid Hook Call - React - Invalid Hook Call: convert App component to functional component, still get Invalid Hook Call 使用函数组件中的 useState 反应无效的挂钩调用错误 - React Invalid Hook Call Error with useState In Functional Component 在功能组件中使用 useSelector 时挂钩调用无效 - Invalid Hook Call when using useSelector in Functional Component 功能元素中的无效 Hook 调用 - Invalid Hook call in a functional element 反应 formik 错误:无效挂钩调用。 钩子只能在功能组件内部调用。 这可能由于以下原因之一而发生 - react formik Error: invalid hook call. Hooks can only be called inside of a functional component. This could happen for one of the following reasons React 不会将我的 Context 组件视为功能组件。 收到无效挂钩调用错误 - React won't acknowledge my Context component as functional component. getting invalid hook call error 从 npm 模块导入功能性反应组件返回无效的钩子调用 - Importing functional react component from npm modules returns invalid hook call Axios 调用在反应功能组件中运行两次 - Axios call runs twice in react functional component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM