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 hook warning when using MDX #12802

Open
1 task
olafsulich opened this issue Dec 20, 2024 · 7 comments
Open
1 task

React hook warning when using MDX #12802

olafsulich opened this issue Dec 20, 2024 · 7 comments
Labels
- P2: has workaround Bug, but has workaround (priority) pkg: mdx Issues pertaining to `@astrojs/mdx` integration pkg: react Related to React (scope)

Comments

@olafsulich
Copy link

Astro Info

Astro                    v5.1.1
Node                     v22.4.0
System                   macOS (arm64)
Package Manager          npm
Output                   static
Adapter                  none
Integrations             @astrojs/react
                         @astrojs/tailwind
                         @astrojs/mdx

If this issue only occurs in one browser, which browser is a problem?

No response

Describe the Bug

I'm getting the react hooks warning on route change. It only shows up when I have enabled the MDX integration.

Related:

The whole message:

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

What's the expected result?

No warnings in the console

Link to Minimal Reproducible Example

https://stackblitz.com/edit/github-syektoew?file=astro.config.mjs

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 Dec 20, 2024
@dervism
Copy link

dervism commented Dec 21, 2024

Having the same issue with 5.1.1. With the MDX-integration enabled I get this error:

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

Removing the mdx-integration and the error messages is gone.

@ascorbic
Copy link
Contributor

Try running npm ls react to see if you have multiple copies of the package. That's usually the cause for these kinds of error.

@olafsulich
Copy link
Author

@ascorbic, it looks like two react but with the same version (?). Here's how it looks for the example:
Zrzut ekranu 2024-12-23 o 20 15 39

@CodeAndWeb
Copy link

CodeAndWeb commented Dec 26, 2024

Same here. I've created a clean project for that. As soon as react and mdx are both present in the astro config, using a hook triggers the warning.

Changing the order of the integration does not seem to have any influence.

npm ls react:

helpless-horizon@0.0.1 /Users/sk/Programming/_tmp/astro-react-test/helpless-horizon
├─┬ @astrojs/react@4.1.2
│ └── react@19.0.0 deduped
├─┬ react-dom@19.0.0
│ └── react@19.0.0 deduped
└── react@19.0.0

The waning only appears on the command line, not in the browser.

Reproduce:

https://github.com/CodeAndWeb/astro-react-mdx-hook-error

@craftgear

This comment has been minimized.

@ascorbic ascorbic added - P2: has workaround Bug, but has workaround (priority) pkg: mdx Issues pertaining to `@astrojs/mdx` integration and removed needs triage Issue needs to be triaged labels Jan 3, 2025
@ascorbic
Copy link
Contributor

ascorbic commented Jan 3, 2025

The error is coming from check() in @astrojs/mdx, here: https://github.com/withastro/astro/blob/main/packages/integrations/mdx/src/server.ts#L10

@bluwy
Copy link
Member

bluwy commented Jan 3, 2025

It seems to be related to React 19. I don't get any warnings with React 18. Related: #12686

@bluwy bluwy added the pkg: react Related to React (scope) label Jan 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
- P2: has workaround Bug, but has workaround (priority) pkg: mdx Issues pertaining to `@astrojs/mdx` integration pkg: react Related to React (scope)
Projects
None yet
Development

No branches or pull requests

6 participants