adminjs icon indicating copy to clipboard operation
adminjs copied to clipboard

Invalid hook call on login page

Open ItsYou opened this issue 5 years ago • 25 comments

Describe the bug I'm getting the following error:

(node:18527) UnhandledPromiseRejectionWarning: 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
See https://fb.me/react-invalid-hook-call for tips about how to debug and fix this problem.
    at resolveDispatcher (/Users/david/workspace/dongwon-api/node_modules/react/cjs/react.development.js:1465:13)
    at Object.useState (/Users/david/workspace/dongwon-api/node_modules/react/cjs/react.development.js:1496:20)
    at ae (/Users/david/workspace/dongwon-api/node_modules/styled-components/dist/styled-components.cjs.js:1:13232)
    at processChild (/Users/david/workspace/dongwon-api/node_modules/admin-bro/node_modules/react-dom/cjs/react-dom-server.node.development.js:3043:14)
    at resolve (/Users/david/workspace/dongwon-api/node_modules/admin-bro/node_modules/react-dom/cjs/react-dom-server.node.development.js:2960:5)
    at ReactDOMServerRenderer.render (/Users/david/workspace/dongwon-api/node_modules/admin-bro/node_modules/react-dom/cjs/react-dom-server.node.development.js:3435:22)
    at ReactDOMServerRenderer.read (/Users/david/workspace/dongwon-api/node_modules/admin-bro/node_modules/react-dom/cjs/react-dom-server.node.development.js:3373:29)
    at renderToString (/Users/david/workspace/dongwon-api/node_modules/admin-bro/node_modules/react-dom/cjs/react-dom-server.node.development.js:3988:27)
    at html (/Users/david/workspace/dongwon-api/node_modules/admin-bro/lib/frontend/login-template.js:73:53)
    at processTicksAndRejections (internal/process/task_queues.js:94:5)
    at async /Users/david/workspace/dongwon-api/node_modules/@admin-bro/express/lib/authentication/login.handler.js:19:23

Installed libraries and their versions

To Reproduce Steps to reproduce the behavior:

  1. Use buildAuthenticatedRouter to create AdminBro router.
  2. Run the app & visit /admin

Additional context There's a closed issue about this matter, but I was not able to resolve this problem after removing node_modules or by adding react & react-dom as dependencies.

ItsYou avatar Apr 11 '21 13:04 ItsYou

Just confirming, are you using admin-bro-expressjs or @admin-bro/express?

Also, what's the version of your @admin-bro/design-system?

dziraf avatar Apr 11 '21 13:04 dziraf

@dziraf My mistake, I'm using @admin-bro/express^3.1.0, and @admin-bro/design-system version is 1.7.0.

ItsYou avatar Apr 11 '21 13:04 ItsYou

I will try to reproduce this error tomorrow. I DID get this error before, but only when developing admin-bro packages by yarn link-ing them though.

Two extra questions:

  1. Is it an existing project and this is a new error or you've only just setup admin bro?
  2. Could you check what's the package version of React in: /node_modules/react/package.json and /node_modules/@admin-bro/design-system/node_modules/react/package.json /node_modules/admin-bro/node_modules/react/package.json It should be at the top.

dziraf avatar Apr 11 '21 13:04 dziraf

First off, thanks for looking into this issue. To answer your questions,

  1. I started using admin-bro recently but just recently set up buildAuthenticatedRouter and encountered this error.
  2. react version in /node_modules/react is 16.13.1, node_modules/@admin-bro/design-system is 16.13.1, and finally in node_modules/admin-bro is also 16.13.1.

To provide a bit more context, I'm using a custom dashboard but I don't think it's related to this as the hook errors show up even when I'm using the vanilla dashboard. I'm starting to assume that this doesn't have to do with react/react-dom version mismatch.

ItsYou avatar Apr 11 '21 14:04 ItsYou

I tried to reproduce on a clean project but I'm not experiencing this problem there.

Are you able to set up a simple repository where this error occurs for you so that I can clone it?

Also, we've released [email protected] - I doubt updating will help in this case but is worth trying

dziraf avatar Apr 15 '21 20:04 dziraf

I ended up fixing this problem. These are the steps I took:

  1. Removed node_modules completely again
  2. Deleted react & react-dom from dependencies in my package.json
  3. Ran npm install again
  4. On install, npm was complaining about missing peer dependencies for admin-bro like before (react, react-dom, etc).
  5. Used install-peerdeps on admin-bro packages.
  6. Works like a charm

ItsYou avatar Apr 19 '21 14:04 ItsYou

After trying everything that has been mentioned about this error, it continues to persist... I don't know what to do...

asakoulas avatar May 17 '21 03:05 asakoulas

This error disappears when I use [email protected]....

asakoulas avatar May 18 '21 00:05 asakoulas

Happened to me on buildAuthenticatedRouter: "@admin-bro/design-system": "^1.7.3", "@admin-bro/express": "^3.1.0", "@admin-bro/mongoose": "^1.1.0", "@admin-bro/passwords": "^1.0.3", "admin-bro": "^4.0.1", "express-formidable": "^1.2.0", "express-session": "^1.17.1", "react": "^17.0.2", "react-dom": "^17.0.2" Using yarn as package manager.

yarikpwnzer avatar May 18 '21 19:05 yarikpwnzer

Fixed by using same react/react-dom versions as in admin-bro dependencies: "react": "=16.13.1", "react-dom": "=16.13.1"

yarikpwnzer avatar May 18 '21 19:05 yarikpwnzer

@yarikpwnzer Well I tried with the exact same version but it doesn't work either. It only works if I use [email protected].

asakoulas avatar May 18 '21 22:05 asakoulas

What is the difference between admin-bro-expressjs an @admin-bro/express ??

asakoulas avatar May 19 '21 00:05 asakoulas

I was just playing with the packages (deleting and re-downloading them and changing their versions) and it just started working. Weird... sorry for all of the comments

asakoulas avatar May 19 '21 01:05 asakoulas

@kodesak I opened yarn.lock and checked versions of react/react-dom and what I noticed that there were two entries: one was added by me (17.0.2) and another was dependency of admin-bro (16.13.1). It worked also without adding it to my dependencies, but since I'm using eslint I got errors in the code.

yarikpwnzer avatar May 19 '21 07:05 yarikpwnzer

MultipleReact I am getting the same error. It works totally fine with local server but when I try to run in the Testing or Staging server it throws this error. I have referred all the solutions regarding this from https://reactjs.org/warnings/invalid-hook-call-warning.html. I think I have multiple react as shown in attached image but I am not able to figure out how can I correct the tree or solve it. Thanks in advance!

RudrikaJnext avatar Jun 14 '21 13:06 RudrikaJnext

At this point, I am probably going to abandon this package because of this error that is stopping me every time...

asakoulas avatar Jun 18 '21 03:06 asakoulas

I get this issue when i have React version ^17.0.2 in my dependencies. If i change it to adminjs version 16.13.1 the issue disappears. But then my Next.JS SSR stops working (monorepo). I need it to work with my main React dependency being of version ^17.0.2 somehow. @dziraf Could you suggest a solution please?

mkayander avatar Aug 14 '21 21:08 mkayander

Coming from the error message:

(node:20508) UnhandledPromiseRejectionWarning: 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
See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.
    at resolveDispatcher (C:\Users\maxim\WebstormProjects\portfolio\node_modules\react\cjs\react.development.js:1476:13)
    at Object.useState (C:\Users\maxim\WebstormProjects\portfolio\node_modules\react\cjs\react.development.js:1507:20)
    at ae (C:\Users\maxim\WebstormProjects\portfolio\node_modules\styled-components\src\utils\isStaticRules.js:6:54)
    at processChild (C:\Users\maxim\WebstormProjects\portfolio\node_modules\adminjs\node_modules\react-dom\cjs\react-dom-server.node.development.js:3043:14)
    at resolve (C:\Users\maxim\WebstormProjects\portfolio\node_modules\adminjs\node_modules\react-dom\cjs\react-dom-server.node.development.js:2960:5)
    at ReactDOMServerRenderer.render (C:\Users\maxim\WebstormProjects\portfolio\node_modules\adminjs\node_modules\react-dom\cjs\react-dom-server.node.development.js:3435:22)
    at ReactDOMServerRenderer.read (C:\Users\maxim\WebstormProjects\portfolio\node_modules\adminjs\node_modules\react-dom\cjs\react-dom-server.node.development.js:3373:29)
    at renderToString (C:\Users\maxim\WebstormProjects\portfolio\node_modules\adminjs\node_modules\react-dom\cjs\react-dom-server.node.development.js:3988:27)
    at html (C:\Users\maxim\WebstormProjects\portfolio\node_modules\adminjs\lib\frontend\login-template.js:73:53)
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
    at C:\Users\maxim\WebstormProjects\portfolio\node_modules\@adminjs\express\src\authentication\login.handler.ts:27:19

I think the first probable reason 1. You might have mismatching versions of React and the renderer (such as React DOM) might be the case. Because from the stacktrace above, we see that react-dom is used from the node_modules\adminjs\node_modules\react-dom directory (older version), but then react is used from node_modules\react directory (newer version, which is specified in package.json). That being said, it makes sense why lowering the react version in package.json fixes the issue. Yet i currently don't know why this behaviour appears and how to make it work like the rest of the app does. (Everything in adminjs works fine except the login page)

mkayander avatar Aug 14 '21 21:08 mkayander

Ok so i just wrote "styled-components": "=4.4.1" in my package.json and it started working 😂. From the third line of stacktrace at ae (C:\Users\maxim\WebstormProjects\portfolio\node_modules\styled-components\src\utils\isStaticRules.js:6:54) i saw that it also uses my newest styled-components package, then it goes to my latest React package. I use Yarn. I assume that it saw that my styled-components version and the one in adminjs are compatible (though not the same, i checked) so it linked it to share with adminjs. That somehow caused the usage of another React version i described above. Thats how i understand it, though it's kinda a bit crazy. I would be glad if someone can clear this up more.

mkayander avatar Aug 14 '21 22:08 mkayander

To fix this issue, I switched to yarn and added this in my package.json.

"resolutions": {
    "adminjs/react": "17.0.2",
    "adminjs/react-dom": "17.0.2"
  },

Gr8z avatar Sep 23 '21 11:09 Gr8z