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

前端 未结 1 1498
天命终不由人
天命终不由人 2021-01-13 02:12

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 compon

相关标签:
1条回答
  • 2021-01-13 02:28

    If you are experiencing this issue ONLY on PC, 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

    0 讨论(0)
提交回复
热议问题