Skip to content

Getting invalid hooks error when loading markdown outside of docusaurs's root folder #8091

Closed

Description

@alexandrebodin

Have you read the Contributing Guidelines on issues?

Prerequisites

Description

I don't think it is a Docusaurus problem itself but I'm missing some knowledge on internals to debug my situation.

When trying to import a file outside of the docusaurus folder in a monorepo like

/```mdx-code-block 
import Contributingfrom "@site/../CONTRIBUTING.md"

<Contributing />
/```

I'm getting the basic invalid hook error

Screenshot 2022-09-13 at 10 41 58

It is coming from the useMDXComponents hooks apparently.

So I figured it was a dependency conflict with my monorepo. I can confirm it is a dependency problem by removing the packages to test this.

All my react/react-dom versions are identical, so it looks like it is a problem with react being loaded twice problem.

Since I'm only loading a plain markdown file, I don't understand why that would start conflicting. Do you have some pointers/recommendations to fix this?

Reproducible demo

No response

Steps to reproduce

I couldn't pinpoint the exact problem sadly and need some insights about how docusaurus might be import things

Expected behavior

No hooks error when loading md files outside of docusaurus folder

Actual behavior

Getting the invalid hook error.

Your environment

  • Docusaurus version used:2.1.0
  • Environment name and version (e.g. Chrome 89, Node.js 16.4): node 18
  • Operating system and version (e.g. Ubuntu 20.04.2 LTS): mac os monterey

Self-service

  • I'd be willing to fix this bug myself.

Activity

  1. added
    bug
    status: needs triage
    on Sep 13, 2022
  2. Josh-Cena commented on Sep 13, 2022

    @Josh-Cena
    Collaborator

    Do you have a repo for me to clone? These kinds of errors are almost always dependency management faults, and without a repro it's hard to investigate.

  3. added
    status: needs more information
    and removed
    status: needs triage
    on Sep 13, 2022
  4. alexandrebodin commented on Sep 13, 2022

    @alexandrebodin
    Author

    Sure I pushed a branch on our monorepo so you can have a look :)
    https://github.com/strapi/strapi/tree/chore/transactions.

    I pinpointed it into the helper-plugin package in packages/core. But couldn't find any real problem with the dependencies in there

  5. alexandrebodin commented on Sep 19, 2022

    @alexandrebodin
    Author

    Hi @Josh-Cena Would you have any pointers for me? Maybe some specific webpack config overwrites to force an alias for react to always be the same path 🤔

  6. slorber commented on Sep 29, 2022

    @slorber
    Collaborator

    Hi @alexandrebodin , happy to see Strapi using Docusaurus (as I'm also French :p)

    At first sight your issue to me is due to monorepo hoisting and using multiple versions of React in the monorepo.

    If your imported MDX doc is at the root of your repo, it will likely use the hoisted version of React that you use on other projects

    • contributing.md will use node_modules/react
    • docs/contributing.md will use docs/node_modules/react

    And then you end-up with 2 duplicate versions of React in the same project.


    Unfortunately, I don't know how we can force the resolution to one version of React: that's how Node.js resolve dependency based on the location of files requiring them. I would advise to only import partials from your Docusaurus folder to avoid these problems.

    To avoid duplicating the file, you can simply do cp contributing.md docs/contributing.md as part of your npm install, and add the copy to gitignore? 🤷‍♂️


    Another possibility would be to find a remark plugin that would allow you to "inline" the contributing doc md content inside your original md document: this way MDX will only build a single React component instead of having one React component importing another (which will import the bad React version).

    Not sure it works but take a look at: https://github.com/dotansimha/remark-import-partial


    Hope this makes sense

  7. pdillon commented on Oct 5, 2022

    @pdillon

    I ran into a similar issue, but not with mdx. Not sure if this would work for you, but adding this to plugins in docusaurus.config.js worked for me:

     plugins: [
        () => ({
          name: 'resolve-react',
          configureWebpack() {
            return {
              resolve: {
                alias: {
                  // assuming root node_modules is up from "./packages/<your-docusaurus>
                  react: path.resolve('../../node_modules/react'), 
                },
              },
            };
          },
        }),
        // ... other plugins
  8. slorber commented on Oct 6, 2022

    @slorber
    Collaborator

    yes 👍 good solution

    Wonder if we shouldn't add this in core to ensure Docusaurus users would at most use a single version of React

    Although the path may vary depending on single repo vs more complex monorepo setups, it should be possible to find a solution and ref the React version Docusaurus is actually using

  9. alexandrebodin commented on Nov 8, 2022

    @alexandrebodin
    Author

    The proposed webpack config worked like a charm. Thanks for the tip 👍

    I'm going to go ahead and close this issue now. Thank you for your help

  10. sedghi commented on Sep 26, 2023

    @sedghi

    @pdillon You saved my day, thanks!

  11. removed
    status: needs more information
    on Sep 26, 2023
  12. mrjacobbloom commented on Oct 12, 2023

    @mrjacobbloom

    It took me a while to track this issue down as the solution to my problem , I hope you don't mind if I dump a few search keywords:

    docusaurus monorepo react component directory dependency
    Invalid hook call. Hooks can only be called inside of the body of a function component.
    Cannot read properties of null (reading 'useContext') (reading 'useState') (reading 'useEffect')
    
  13. added a commit that references this issue on May 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions