question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Invalid hook call. Hooks can only be called inside of the body of a function component.

See original GitHub issue

Bug 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:

  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.

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

Screen Shot 2019-06-21 at 12 10 08 PM

Additional context

I suspect that it treats the ctx.renderPage = () => as a react hooks.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:14
  • Comments:24 (7 by maintainers)

github_iconTop GitHub Comments

29reactions
dbeaudwaycommented, Oct 13, 2019

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

13reactions
brycelundcommented, Mar 11, 2020

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,

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found