0

I have developed my own website and have errors using react hook. The errors are like below.

Uncaught Error: 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

And here is my code:

import React from 'react';
import { useSelector } from 'react-redux';

import 'bootstrap/dist/css/bootstrap.min.css';

const ProfileEditing = ({
}) => {

    const user = useSelector(state => state.auth);
    console.log(user);

    return (
        <section className="dashboard">
            <div className='side'>
                <h1 className="large text-primary">Dashboard</h1>
                <p className="lead">
            </div>
        </section>
    );
};

export default ProfileEditing;

I try to fix it but doesn't work at all. Please help me.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Alchemist
  • 325
  • 1
  • 17
  • Can you show us where you use `ProfileEditing`? In particular, are you rendering an element (``) or calling it as a function (`ProfileEditing()`) – Nicholas Tower May 19 '22 at 20:19
  • I call this function. GO And I defined /profileEditing in App.js The point is I use the exactly the same code in other function but it works. – Alchemist May 19 '22 at 20:23
  • `I call this function` To use a component you need to render it as an element, not call it as a function. What's the reason you're calling `ProfileEditing()`? – Nicholas Tower May 19 '22 at 20:25
  • Sorry, I didn't call this function. I link to that function like the code shows. – Alchemist May 19 '22 at 20:27
  • 1
    Thanks, but i don't need to see the link, just how you render ProfileEditing. The code that you showed in your question should not cause the error you saw, as long as you are rendering `` in a normal way. – Nicholas Tower May 19 '22 at 20:28
  • In Dashboard.js I return it. `code` return ( GO ) When I click GO, it occures error. – Alchemist May 19 '22 at 20:34
  • 1
    Again, i don't need to see the link. Are you using react-router? If so, the code i'm looking for would look something like `}/>` – Nicholas Tower May 19 '22 at 20:50
  • Yes, I use react-router and use the code just you write. And I just tried to rendering it but it also throw same error. – Alchemist May 19 '22 at 20:51
  • And If I remove useSelector, then everything is Okay. – Alchemist May 19 '22 at 20:54
  • I render in App.js `return ( } /> ` – Alchemist May 19 '22 at 21:04
  • Unable to reproduce. https://codesandbox.io/s/react-playground-forked-m40uec?file=/index.js – Nicholas Tower May 19 '22 at 21:14
  • Thank you for your care. I see it works but wonder how it works. And I still don't know what is difference. And what do you mean "Unable to reproduce"? – Alchemist May 19 '22 at 21:21
  • I recreated the code (see the link), and the bug does not exist. That's what "unable to reproduce" means. Maybe you can see what's different from your code, but at the moment, i cannot help, because i cannot see the problem. – Nicholas Tower May 19 '22 at 21:22
  • I moved ProfileEditing function to App.js and it works. I can receive datas from store. But once I move back to components folder I got error again. It is really hot potato.. – Alchemist May 19 '22 at 21:40

1 Answers1

1

I fixed error. The problem is not because of code but because of node_modules. I removed package-lock.json and node_modules and reinstall it. And it works now.

Alchemist
  • 325
  • 1
  • 17
  • [A question addressing your issue](https://stackoverflow.com/questions/57971303/react-invalid-hook-call-hooks-can-only-be-called-inside-of-the-body-of-a-funct) was already created 2 years ago. Please DYOR before asking for anything. – l -_- l Jul 09 '22 at 14:19
  • Maybe questions can be same. But same error occures due to new versions and thus answers may different. And it is hard to find updated answer in older questions. In my case, the code was OK. It is just the problem of `node_modules`. I think it is worth than nothing. And the `issue` you gave me has no solved mark. Thanks anyway. – Alchemist Jul 17 '22 at 01:05