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

Can't import the react client component in mdx files. #1021

Open
annder opened this issue Sep 7, 2024 · 3 comments
Open

Can't import the react client component in mdx files. #1021

annder opened this issue Sep 7, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@annder
Copy link

annder commented Sep 7, 2024

Describe the bug
When I wrote a simple counter component and imported it into the mdx file to render as content, Nextjs said that the component was not supported.

To Reproduce
Steps to reproduce the behavior:

  1. Create an mdx file in the data/blog fold and write some text, the content is filled in casually.
  2. Create a react component named test.tsx and write content like:
'use client'

import { useState } from 'react'

const Index = () => {
  const [counter] = useState(0)
  return <div>{counter}</div>
}

export default Index
  1. import the component path into mdx file like this:
import Test from "@/components/Test.tsx"

<Test />
  1. And Nextjs will throw the error saying:useState only works in Client Components. Add the "use client" directive at the top of the file to use it. Read more: https://nextjs.org/docs/messages/react-client-hook-in-server-component

Expected behavior
Successfully imported the component into mdx file and rendered it on the browse.

Screenshots
If applicable, add screenshots to help explain your problem.
image

System Info (if dev / build issue):

  • OS: Windows
  • Node version:20.17.0
  • Npm version:10.8.2
  • Next version:14.2.3

Browser Info (if display / formatting issue):

  • Device PC
  • Browser: Chrome
  • Version :128.0.6613.120

Additional context
I found the same issue in the nextjs issues, but During I attempt to search mdx-components.mdx file, all of the dirs can't be found.
vercel/next.js#47523

@annder annder added the bug Something isn't working label Sep 7, 2024
@timlrx
Copy link
Owner

timlrx commented Sep 12, 2024

This is a known issue with the current setup. As a workaround you can register the MDX components in components/MDXComponents.tsx. See example here.

@MiltonAkash
Copy link

@timlrx Adding things in MDXComponents works fine. But a small doubt. Will it then load all components for every page?

@dexcompiler
Copy link

@timlrx Adding things in MDXComponents works fine. But a small doubt. Will it then load all components for every page?

I presume you can simply use conditional logic to toggle on/off whatever component(s) you want activated/deactivated

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants