未捕获错误:无效的挂钩调用.钩子只能在使用Create React App和Formik时在函数组件的主体内部调用



我收到此错误,我真的不知道我做错了什么。该程序几个小时前运行良好。这在我安装 Formik 后才开始发生,但对我来说一切看起来都很好。代码如下:

import React from "react";
import { useFormik } from "formik";
export default function YoutubeForm() {
const formik = useFormik({
initialValues: {
name: "",
email: "",
channel: "",
},
});
console.log("formik values", formik.values);
return (
<div>
<form>
<label htmlFor="name">Name</label>
<input
type="text"
id="name"
name="name"
onChange={formik.handleChange}
value={formik.values.name}
/>
<label htmlFor="email">Email</label>
<input
type="email"
id="email"
name="email"
onChange={formik.handleChange}
value={formik.values.email}
/>
<label htmlFor="channel">Channel</label>
<input
type="text"
id="channel"
name="channel"
onChange={formik.handleChange}
value={formik.values.channel}
/>
<button>Submit</button>
</form>
</div>
);
}

有人可以向我解释为什么会发生这种情况

吗当我运行"npm ls react"时,我得到这个:

├─┬ @testing库/react@12.1.2 │ └── react@17.0.2 重复数据删除 ├─┬ 反应-dom@17.0.2 │ └── react@17.0.2 重复数据删除 ├─┬ 反应-scripts@5.0.0 │ └── react@17.0.2 重复数据删除 └── react@17.0.2

我不太确定这意味着什么

package.json:

{
"name": "my-app",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.16.1",
"@testing-library/react": "^12.1.2",
"@testing-library/user-event": "^13.5.0",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-scripts": "5.0.0",
"web-vitals": "^2.1.2"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}

我这样做了:

// Add this in node_modules/react-dom/index.js
window.React1 = require('react');
// Add this in your component file
require('react-dom');
window.React2 = require('react');
console.log(window.React1 === window.React2);

它注销了"false",这可能意味着我的应用程序正在使用两个反应

我没有看到安装了formik软件包。尝试安装它

npm i formik

可能是因为您formik全局安装

接下来,请删除node-modulespackage-lock.json然后请运行npm i

最新更新