Invalid hook call on login page
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
- [email protected]
- @admin-bro/[email protected]
- @admin-bro/[email protected]
- [email protected]
- [email protected]
To Reproduce Steps to reproduce the behavior:
- Use
buildAuthenticatedRouterto create AdminBro router. - 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.
Just confirming, are you using admin-bro-expressjs or @admin-bro/express?
Also, what's the version of your @admin-bro/design-system?
@dziraf My mistake, I'm using @admin-bro/express^3.1.0, and @admin-bro/design-system version is 1.7.0.
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:
- Is it an existing project and this is a new error or you've only just setup admin bro?
- Could you check what's the package version of React in:
/node_modules/react/package.jsonand/node_modules/@admin-bro/design-system/node_modules/react/package.json/node_modules/admin-bro/node_modules/react/package.jsonIt should be at the top.
First off, thanks for looking into this issue. To answer your questions,
- I started using admin-bro recently but just recently set up
buildAuthenticatedRouterand encountered this error. - react version in
/node_modules/reactis 16.13.1,node_modules/@admin-bro/design-systemis 16.13.1, and finally innode_modules/admin-brois 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.
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
I ended up fixing this problem. These are the steps I took:
- Removed node_modules completely again
- Deleted react & react-dom from dependencies in my package.json
- Ran npm install again
- On install, npm was complaining about missing peer dependencies for admin-bro like before (react, react-dom, etc).
- Used install-peerdeps on admin-bro packages.
- Works like a charm
After trying everything that has been mentioned about this error, it continues to persist... I don't know what to do...
This error disappears when I use [email protected]....
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.
Fixed by using same react/react-dom versions as in admin-bro dependencies: "react": "=16.13.1", "react-dom": "=16.13.1"
@yarikpwnzer Well I tried with the exact same version but it doesn't work either. It only works if I use [email protected].
What is the difference between admin-bro-expressjs an @admin-bro/express ??
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
@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.
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!
At this point, I am probably going to abandon this package because of this error that is stopping me every time...
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?
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)
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.
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"
},