“useFormik” 不能在顶层调用。

huangapple go评论75阅读模式
英文:

"useFormik" cannot be called at the top level

问题

错误:无法在顶层调用React Hook "useFormik"。React Hooks必须在React函数组件或自定义的React Hook函数中调用。

我将第75行到92行的代码剪切,并粘贴到第5行,但仍然出现相同的错误(如何解决此问题)。

英文:

Error: React Hook "useFormik" cannot be called at the top level. React Hooks must be called in a React function component or a custom React Hook function

“useFormik” 不能在顶层调用。

i cut this line from line 75 to 92 and paste in line 5 but same error (HOW TO SOLVE THIS)

答案1

得分: 1

在这张图片中,我看到你在函数组件外部使用了 useFormik 钩子。但是你必须在一个 React 函数组件 内部使用它。

 // 导入所需的模块和库
 import React from 'react';
 import { useFormik } from 'formik';
 
// 一个 React 函数组件
 const SignupForm = () => {
   // 这里它在 `SignupForm` 组件内部
   // useFormik 钩子开始
   const formik = useFormik({
     initialValues: {
       firstName: '',
       lastName: '',
       email: '',
     },
     onSubmit: values => {
       alert(JSON.stringify(values, null, 2));
     },
   });
   // useFormik 钩子结束

   // 渲染表单
   return (
     <form onSubmit={formik.handleSubmit}>
       <label htmlFor="firstName">First Name</label>
       <input
         id="firstName"
         name="firstName"
         type="text"
         onChange={formik.handleChange}
         value={formik.values.firstName}
       />
       <label htmlFor="lastName">Last Name</label>
       <input
         id="lastName"
         name="lastName"
         type="text"
         onChange={formik.handleChange}
         value={formik.values.lastName}
       />
       <label htmlFor="email">Email Address</label>
       <input
         id="email"
         name="email"
         type="email"
         onChange={formik.handleChange}
         value={formik.values.email}
       />
       <button type="submit">Submit</button>
     </form>
   );
 };
英文:

In the picture, I see you have used the useFormik hook outside the function component. But you have to use this inside a React Function Component

 // import required modules &amp; libraries
 import React from &#39;react&#39;;
 import { useFormik } from &#39;formik&#39;;
 
// A react function component
 const SignupForm = () =&gt; {
   // here it is inside the `SignupForm` component
   // useFormik Hook Start
   const formik = useFormik({
     initialValues: {
       firstName: &#39;&#39;,
       lastName: &#39;&#39;,
       email: &#39;&#39;,
     },
     onSubmit: values =&gt; {
       alert(JSON.stringify(values, null, 2));
     },
   });
   // useFormik Hook End

   // Rnder the form
   return (
     &lt;form onSubmit={formik.handleSubmit}&gt;
       &lt;label htmlFor=&quot;firstName&quot;&gt;First Name&lt;/label&gt;
       &lt;input
         id=&quot;firstName&quot;
         name=&quot;firstName&quot;
         type=&quot;text&quot;
         onChange={formik.handleChange}
         value={formik.values.firstName}
       /&gt;
       &lt;label htmlFor=&quot;lastName&quot;&gt;Last Name&lt;/label&gt;
       &lt;input
         id=&quot;lastName&quot;
         name=&quot;lastName&quot;
         type=&quot;text&quot;
         onChange={formik.handleChange}
         value={formik.values.lastName}
       /&gt;
       &lt;label htmlFor=&quot;email&quot;&gt;Email Address&lt;/label&gt;
       &lt;input
         id=&quot;email&quot;
         name=&quot;email&quot;
         type=&quot;email&quot;
         onChange={formik.handleChange}
         value={formik.values.email}
       /&gt;
       &lt;button type=&quot;submit&quot;&gt;Submit&lt;/button&gt;
     &lt;/form&gt;
   );
 };

答案2

得分: 0

你需要做类似这样的操作:

const MyForm = (handleSubmit, setValues, ...) => { 
    // 需要像这样使用这个 hook 
    const formik = useFormik({
        initialValues: {
            NameOftheUPSIProject: '',
            InformationSharedBy: '',
            InsiderTypes: '',
            ..
        },
        onSubmit: values => {
            console.log(JSON.stringify(values));
        },
    });
}

如上所示的代码中,你可以将 useFormik hook 放在 React 的 functional 组件或 React 的 hook 函数内部使用。

参考文档: useFormik

英文:

You need to do something like this:

 const MyForm = (handleSubmit, setValues, ...) =&gt; { 
// the hook needs to be used like this 
const formik = useFormik({
initialValues: {
NameOftheUPSIProject: &#39;&#39;,
InformationSharedBy: &#39;&#39;,
InsiderTypes: &#39;&#39;,
..
},
onSubmit: values =&gt; {
console.log(JSON.stringify(values));
},
});
} 

As seen in the above code, you can use the useFormik hook either inside a React functional component or a React hook function.

Reference doc: useFormik

huangapple
  • 本文由 发表于 2023年5月10日 12:26:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76214885.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定