React in MDX and MDX in react
You can add React components inside of MDX content, but the other way around works too. In this part of the tutorial, we will explore a bit how to use components in MDX, but also MDX in components
React components in MDX content
Of course, the big advantage of MDX over regular markdown is that we can use any React component inside of our MDX content
we are about to create several folders and then add a file, so you might start by creating the first folder and then inside of it the second folder...
But did you know, that you can also right click inside of the File Explorer (in the primary sidebar) and then chose New File... and enter the whole components/tutorial_examples/buttons/Counting.tsx
path at once, VSCode will create all of the folders as well as the file (all in one go)
First, create a new folder components
in the root of your project, then inside the components
folder, create a tutorial_examples
folder, and inside that folder, another buttons
folder
Now, inside of the buttons
folder, create a Counting.tsx
file and insert the following code:
Now let's edit the /app/(tutorial_examples)/first_mdx_page/page.mdx
page we created earlier and add our Counting component to it, like so:
Did you notice how we did NOT use the relative path that would be '../components/tutorial_examples/buttons/Counting'?
Instead, we used the @/ alias, which we added when creating the project (using creat-next-app)
The advantage of using the alias is that no matter where we move our page, the path to the Button will always stay the same (of course, if we move the button, then we also need to update the import)
Make sure your dev server is running (if it is not, start it using npm run dev
)
Then visit the updated MDX page in the browser at http://localhost:3000/first_mdx_page
and try out the counting button
Of course, this is just an example, you can import any component you want, for example, a chart component to visualize some statistics or a component displaying a 3d model of an object
MDX in pages and components
Besides turning a whole page into an MDX page, as we saw in the previous chapter, you can also import MDX into typescript (.tsx) pages and React components
import MDX content into a typescript page
First, inside of the /app/(tutorial_examples)
folder, create a new mdx_in_a_page
folder
Then, in the mdx_in_a_page
folder, create a file called content.mdx
with the following content:
And then, in the same folder, create a page.tsx
with the following content:
Make sure your dev server is running, if it is not, start it using npm run dev
Then visit your newly created page in the browser at http://localhost:3000/mdx_in_a_page
import MDX into a component
First, go into the /components/tutorial_examples
folder and then create another folder called mdx
Inside the mdx
folder, create a content.mdx
file and insert the following content:
And then, in the same folder, create an Example.tsx
file with the following content:
Then, inside of the /app/(tutorial_examples)
folder, create another new folder, mdx_in_a_component
In the mdx_in_a_component
folder, create a page.tsx
file with the following content:
Make sure your dev server is running, if it is not, start it using npm run dev
Then visit your newly created MDX page in the browser at http://localhost:3000/mdx_in_a_component