Description of the Error:
I want to add a component in the mdx file. I’m using the blog starter kit and I don’t understand how to import a component in the post.mdx.
This is what happened:
Error: Expected component Badge to be defined: you likely forgot to import, pass, or provide it.
It’s referenced in your code at 44:1-47:3
I read the documentation but I don’t understand why. Can someone help me?
I need to specify that the badge.tsx on the Gist is equal to mdx-components.tsx on my repo. I wanted to have a file where I can put the components that I made on the post mdx.
Ah sorry I think I see the issue. In your mdx-components file you need to export the function useMDXComponents returning the list of your components.
So for structure I’d place your badge in its own component. This can live in your components folder
then import it into mdx-components.
eg:
import Badge from '../components/Badge
export function useMDXComponents() {
return {
Badge,
// Add any other components you want to use in MDX here
}
}
Here are the docs on this. Let me know if this works
I think I need to understand better this topic about mdx in nextjs
If you prefer I can push the source code on github in a new branch so everyone can understand better.
Alright I think I figured it out and got it building locally. Not sure if this is from the template or not but it looks like you’re using a mdx.tsx component file, passing in CustomMDX, and exporting your custom components from there. To fix the error you’ll need to import badge.tsx and assign it to the components object.
From here you can remove the mdx-components global file since you’re using a different approach. Again not sure if this is legacy from a template, but I’d eventually recommend using the mdx-components approach instead as it makes your components available to all MDX files automatically and is also how Next .js recommends.
Oh also you can remove the import from the post mdx file itself since your making the components available through the above approach.