Uncaught Error: Invalid hook call. Hooks can only be called inside of the body of a function component when using Create React App and Formik

Uncaught Error: Invalid hook call. Hooks can only be called inside of the body of a function component when using Create React App and 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-library/react@12.1.2 │ └── react@17.0.2 去重 ├─┬反应-dom@17.0.2 │ └── react@17.0.2 去重 ├─┬ react-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”,这可能意味着我的应用程序正在使用两个 Reacts

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

npm i formik

可能是因为您在全局安装了 formik

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