Home > database >  Uncaught Error: Invalid hook call. Hooks can only be called inside of the body of a function compone
Uncaught Error: Invalid hook call. Hooks can only be called inside of the body of a function compone

Time:12-22

Im getting this error and I seriously dont know what Im doing wrong. The program worked just fine a few hours ago. This only began happening after I installed Formik but everything looks okay to me. Here's the code:

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>
    );
}

Can somebody please explain to me why this is happening

I get this when I run "npm ls react":

├─┬ @testing-library/[email protected] │ └── [email protected] deduped ├─┬ [email protected] │ └── [email protected] deduped ├─┬ [email protected] │ └── [email protected] deduped └── [email protected]

I'm not really sure what any of this means

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"
    ]
  }
}

I did this:

// 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);

and it logged out "false" which could mean that my app is using two Reacts

CodePudding user response:

I don't see formik package installed. Try to install it

npm i formik

It might be because you have formik installed globally

Next, please delete node-modules and package-lock.json and then please run npm i

  • Related