[英]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:
该错误指出只能在功能组件的主体内部调用钩子,因此:
//body of functional component here
//body of functional component here
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
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
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.