Invalid hook call. Hooks can only be called inside of the body of a function component.
See original GitHub issueBug report
Describe the bug
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:
- You might have mismatching versions of React and the renderer (such as React DOM)
- You might be breaking the Rules of Hooks
- 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.
To Reproduce
Details
So the error points on this code
static async getInitialProps (ctx) {
const sheet = new ServerStyleSheet()
const originalRenderPage = ctx.renderPage
const {
req: { locale, localeDataScript }
} = ctx
try {
ctx.renderPage = () =>
originalRenderPage({
enhanceApp: App => props => sheet.collectStyles(<App {...props} />)
})
const initialProps = await Document.getInitialProps(ctx)
return {
...initialProps,
userAgent: ctx.req.headers['user-agent'],
helmet: Helmet.renderStatic(),
locale,
localeDataScript,
styles: (
<>
{initialProps.styles}
{sheet.getStyleElement()}
</>
)
}
} finally {
sheet.seal()
}
}
Screenshots

Additional context
I suspect that it treats the ctx.renderPage = () => as a react hooks.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:14
- Comments:24 (7 by maintainers)
Top Results From Across the Web
Invalid Hook Call Warning - React
Hooks can only be called inside the body of a function component. There are three common reasons you might be seeing it: You...
Read more >Invalid hook call. Hooks can only be called inside of the body ...
Invalid hook call. Hooks can only be called inside of the body of a function component · You might have mismatching versions of...
Read more >Invalid hook call. Hooks can only be called inside of the body ...
Like others have said: hooks can only be called in the body of a function component, you cannot call them conditionally or in...
Read more >Invalid hook call. Hooks can only be called inside the body of ...
Having a mismatch between the versions of react and react-dom . · Having multiple versions of the react package in the same project....
Read more >Invalid hook call. Hooks can only be called ... - Datainfinities
The error "Invalid hook call. Hooks can only be called inside the body of a function component" occurs due to the following reasons....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

For those who are still encountering this error after troubleshooting all the proposed fixes, double-check that you are using the correct directory casing when running your project. I continued to have problems on my PC and discovered this was the reason why.
https://stackoverflow.com/questions/58365151/hooks-error-invalid-hook-call-using-nextjs-or-reactjs-on-windows
I had a node_modules folder in a parent directory up the tree that was causing issues. I deleted that and it started working as expected,