Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hooks error: Invalid Hook Call using NextJS or ReactJS on Windows

While attempting to use React Hooks in NextJS I continue to receive the following error:

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

This issue only occurs on Windows and works fine using Mac.

This is a well documented issue and I have troubleshot many of the proposed solutions, including:

  • Multiple versions of React or React DOM
  • Modifying webpack settings in next.config.js
  • Linking to specific node module pacakges

Here is an example of use:

import React, { useState, useEffect } from 'react'

const Index = () => {
   const [ test, setTest ] = useState('Test')

   useEffect(() => (
      console.log(`Page loaded, variable value: ${test}`)
   ), [])

   return <div>Hello</div>
}

I am using the following versions:

"next": "^9.1.1",
"react": "^16.10.2",
"react-dom": "^16.10.2"
like image 598
David Beaudway Avatar asked Oct 13 '19 15:10

David Beaudway


People also ask

How do you fix error invalid hook call Hooks can only be called inside of the body of a function component?

Hooks can only be called inside the body of a function component. There are three common reasons you might be seeing it: You might have mismatching versions of React and React DOM. You might be breaking the Rules of Hooks.

Can I use React Hooks in next JS?

getStaticProps technically has nothing to do with React. In essence that is just a function with a special meaning and functionality to the Next. js framework itself. So no, you can't use hooks in there (at least not the ones that use setState useEffect etc..

What is useEffect () hook?

The useEffect Hook allows you to perform side effects in your components. Some examples of side effects are: fetching data, directly updating the DOM, and timers. useEffect accepts two arguments. The second argument is optional.

What is a hook call React?

Hooks are functions that let you “hook into” React state and lifecycle features from function components. Hooks don't work inside classes — they let you use React without classes. (We don't recommend rewriting your existing components overnight but you can start using Hooks in the new ones if you'd like.)


1 Answers

If you are experiencing this issue ONLY on Windows, it may have to do with how you navigate to your project folder. Make sure you are using the correct character case on all of your folders! In my scenario, I used Powershell to navigate to my project and run the development server.

For example, the following will result in a Hook error:

cd desktop/project_folder

npm run dev

The issue is that "desktop" is a capitalized directory name. Therefore, running the server with the following steps works perfectly:

cd Desktop/project_folder

npm run dev

This issue also occurs if you opened your project_folder using incorrect casing and then use an interactive shell to run the development server, for example:

cd desktop/project_folder

## Open project_folder in VS Code
code .

## Start development server from VS Code's interactive shell
npm run dev

At the root of the issue, I believe module paths are incorrectly being imported due to a variation of expected path names. If you see the following warning coming from your development server, make sure to address it first:

There are multiple modules that exist that only differ in casing

like image 72
David Beaudway Avatar answered Oct 15 '22 05:10

David Beaudway