Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

React invalid hook call warning while using mdx and react integrations at the same time #8317

Closed
1 task done
rk-terence opened this issue Aug 31, 2023 · 5 comments · Fixed by #8324
Closed
1 task done
Assignees
Labels
- P5: urgent Fix build-breaking bugs affecting most users, should be released ASAP (priority)

Comments

@rk-terence
Copy link

rk-terence commented Aug 31, 2023

Astro info

Astro version            v3.0.3
Package manager          pnpm
Platform                 darwin
Architecture             arm64
Adapter                  Couldn't determine.
Integrations             @astrojs/mdx, @astrojs/react

What browser are you using?

Safari

Describe the Bug

When opening localhost:4321/ in my browser, the dev server will log the following error, if we use both the @astrojs/mdx and @astrojs/react integrations.

Warning: 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://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.

There will be no warning if I first build the project and run astro preview.

After some digging, I have found that this is related to the astro:jsx renderer. When running this renderer's check function, the warning will be logged. In addition, removing the @astrojs/mdx integration also fixes this.

What's the expected result?

It would be better if no warning is shown in the console during development.

Link to Minimal Reproducible Example

https://github.com/rk-terence/mre-invalid-hook-call-in-react

Participation

  • I am willing to submit a pull request for this issue.
@github-actions github-actions bot added the needs triage Issue needs to be triaged label Aug 31, 2023
@rk-terence
Copy link
Author

rk-terence commented Aug 31, 2023

I have checked #4220, but using export default function Blabla () {} syntax does not fix it.

@rk-terence
Copy link
Author

rk-terence commented Aug 31, 2023

Also, if in astro.config.mjs file we change the order of mdx and react to

export default defineConfig({
  integrations: [react(), mdx()]
});

the warning will disappear.

@matthewp
Copy link
Contributor

Thanks! The astro:jsx renderer is a special-case and we should probably make it so that order doesn't matter in this case. I'll fix this.

@matthewp matthewp self-assigned this Aug 31, 2023
@matthewp matthewp added - P5: urgent Fix build-breaking bugs affecting most users, should be released ASAP (priority) and removed needs triage Issue needs to be triaged labels Aug 31, 2023
@dirkjf
Copy link

dirkjf commented Apr 22, 2024

@matthewp, this issue appears to be back. My config looks like this:

export default defineConfig({
  plugins: ["prettier-plugin-astro"],
  integrations: [
    react(), mdx()
  ],
  i18n,
  routing: {
    prefixDefaultLocale: false
  },
  vite: {
    build: {
      assetsInlineLimit: 0
    },
    plugins: [
      react(),
      svgr()
    ]
  }
});

Using

astro 4.6.3
@astrojs/mdx: 2.3.1
@astrojs/react: 3.3.0

Removing the mdx integration the warning goes away.

@DarrenBaldwin07
Copy link

@matthewp, this issue appears to be back. My config looks like this:

export default defineConfig({
  plugins: ["prettier-plugin-astro"],
  integrations: [
    react(), mdx()
  ],
  i18n,
  routing: {
    prefixDefaultLocale: false
  },
  vite: {
    build: {
      assetsInlineLimit: 0
    },
    plugins: [
      react(),
      svgr()
    ]
  }
});

Using

astro 4.6.3
@astrojs/mdx: 2.3.1
@astrojs/react: 3.3.0

Removing the mdx integration the warning goes away.

This is happening to me too and my integrations are in this order: [react(), mdx()]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
- P5: urgent Fix build-breaking bugs affecting most users, should be released ASAP (priority)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants