--- id: react description: Using the power of React in Docusaurus Markdown documents, thanks to MDX slug: /markdown-features/react --- # MDX and React ```mdx-code-block import BrowserWindow from '@site/src/components/BrowserWindow'; import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem'; import styles from './markdown-features-react.module.css'; ``` Docusaurus has built-in support for [MDX](https://mdxjs.com/), which allows you to write JSX within your Markdown files and render them as React components. Check out the [MDX docs](https://mdxjs.com/) to see what fancy stuff you can do with MDX. :::tip Debugging MDX The MDX format is quite strict, and you may get compilation errors. Use the **[MDX playground](https://mdxjs.com/playground/)** to debug them and make sure your syntax is valid. ::: :::info Prettier, the most popular formatter, [supports only the legacy MDX v1](https://github.com/prettier/prettier/issues/12209). If you get an unintentional formatting result, you may want to add `{/* prettier-ignore */}` before the problematic area, or add `*.mdx` to your `.prettierignore`, until Prettier has proper support for MDX v3. [One of the main authors of MDX recommends `remark-cli` with `remark-mdx`](https://github.com/orgs/mdx-js/discussions/2067). ::: ### Exporting components {#exporting-components} To define any custom component within an MDX file, you have to export it: only paragraphs that start with `export` will be parsed as components instead of prose. ```jsx export const Highlight = ({children, color}) => ( {children} ); Docusaurus green and Facebook blue are my favorite colors. I can write **Markdown** alongside my _JSX_! ``` Notice how it renders both the markup from your React component and the Markdown syntax: ```mdx-code-block export const Highlight = ({children, color}) => ( {children} ); <>Docusaurus green {` `}and Facebook blue are my favorite colors. I can write **Markdown** alongside my _JSX_! ``` :::warning MDX is JSX Since all doc files are parsed using MDX, anything that looks like HTML is actually JSX. Therefore, if you need to inline-style a component, follow JSX flavor and provide style objects. {/* prettier-ignore */} ```jsx /* Instead of this: */ Foo /* Use this: */ Foo ``` ::: ### Importing components {#importing-components} You can also import your own components defined in other files or third-party components installed via npm. {/* prettier-ignore */} ```md import TOCInline from '@theme/TOCInline'; import Button from '@mui/material/Button'; import BrowserWindow from '@site/src/components/BrowserWindow'; ``` :::tip The `@site` alias points to your website's directory, usually where the `docusaurus.config.js` file is. Using an alias instead of relative paths (`'../../src/components/BrowserWindow'`) saves you from updating import paths when moving files around, or when [versioning docs](../docs/versioning.mdx) and [translating](../../i18n/i18n-tutorial.mdx). ::: While declaring components within Markdown is very convenient for simple cases, it becomes hard to maintain because of limited editor support, risks of parsing errors, and low reusability. Use a separate `.js` file when your component involves complex JS logic: ```jsx title="src/components/Highlight.js" import React from 'react'; export default function Highlight({children, color}) { return ( {children} ); } ``` ```md title="markdown-file.mdx" import Highlight from '@site/src/components/Highlight'; Docusaurus green ``` :::tip If you use the same component across a lot of files, you don't need to import it everywhere—consider adding it to the global scope. [See below](#mdx-component-scope) ::: ### MDX component scope {#mdx-component-scope} Apart from [importing a component](#importing-components) and [exporting a component](#exporting-components), a third way to use a component in MDX is to **register it to the global scope**, which will make it automatically available in every MDX file, without any import statements. For example, given this MDX file: ```md - a - list! And some custom markup... ``` It will be compiled to a React component containing `ul`, `li`, `p`, and `Highlight` elements. `Highlight` is not a native html element: you need to provide your own React component implementation for it. In Docusaurus, the MDX component scope is provided by the `@theme/MDXComponents` file. It's not a React component, _per se_, unlike most other exports under the `@theme/` alias: it is a record from tag names like `Highlight` to their React component implementations. If you [swizzle](../../swizzling.mdx) this component, you will find all tags that have been implemented, and you can further customize our implementation by swizzling the respective sub-component, like `@theme/MDXComponents/Code` (which is used to render [Markdown code blocks](./markdown-features-code-blocks.mdx)). If you want to register extra tag names (like the `` tag above), you should consider [wrapping `@theme/MDXComponents`](../../swizzling.mdx#wrapping), so you don't have to maintain all the existing mappings. Since the swizzle CLI doesn't allow wrapping non-component files yet, you should manually create the wrapper: ```js title="src/theme/MDXComponents.js" import React from 'react'; // Import the original mapper import MDXComponents from '@theme-original/MDXComponents'; // highlight-next-line import Highlight from '@site/src/components/Highlight'; export default { // Re-use the default mapping ...MDXComponents, // Map the "" tag to our Highlight component // `Highlight` will receive all props that were passed to `` in MDX // highlight-next-line Highlight, }; ``` And now, you can freely use `` in every page, without writing the import statement: ```md I can conveniently use Docusaurus green everywhere! ``` ```mdx-code-block I can conveniently use Docusaurus green everywhere! ``` :::warning We use **upper-case** tag names like `Highlight` on purpose. From MDX v3+ onward (Docusaurus v3+), lower-case tag names are always rendered as native html elements, and will not use any component mapping you provide. ::: :::warning This feature is powered by [an `MDXProvider`](https://mdxjs.com/docs/using-mdx/#mdx-provider). If you are importing Markdown in a React page, you have to supply this provider yourself through the `MDXContent` theme component. ```jsx title="src/pages/index.js" import React from 'react'; import FeatureDisplay from './_featureDisplay.mdx'; // highlight-next-line import MDXContent from '@theme/MDXContent'; export default function LandingPage() { return (
{/* highlight-start */} {/* highlight-end */}
); } ``` If you don't wrap your imported MDX with `MDXContent`, the global scope will not be available. ::: ### Markdown and JSX interoperability {#markdown-and-jsx-interoperability} Docusaurus v3 is using [MDX v3](https://mdxjs.com/blog/v3/). The [MDX syntax](https://mdxjs.com/docs/what-is-mdx/#mdx-syntax) is mostly compatible with [CommonMark](https://commonmark.org/), but is much stricter because your `.mdx` files can use JSX and are compiled into real React components (check the [playground](https://mdxjs.com/playground/)). Some valid CommonMark features won't work with MDX ([more info](https://mdxjs.com/docs/what-is-mdx/#markdown)), notably: - Indented code blocks: use triple backticks instead - Autolinks (``): use regular link syntax instead (`[http://localhost:3000](http://localhost:3000)`) - HTML syntax (`

`): use JSX instead (`

`) - Unescaped `{` and `<`: escape them with `\` instead (`\{` and `\<`) :::danger Experimental CommonMark support Docusaurus v3 makes it possible to opt-in for a less strict, standard [CommonMark](https://commonmark.org/) support with the following options: - The `format: md` front matter - The `.md` file extension combined with the `siteConfig.markdown.format: "detect"` configuration This feature is **experimental** and currently has a few [limitations](https://github.com/facebook/docusaurus/issues/9092). ::: ## Importing code snippets {#importing-code-snippets} You can not only import a file containing a component definition, but also import any code file as raw text, and then insert it in a code block, thanks to [Webpack raw-loader](https://webpack.js.org/loaders/raw-loader/). In order to use `raw-loader`, you first need to install it in your project: ```bash npm2yarn npm install --save raw-loader ``` Now you can import code snippets from another file as it is: {/* prettier-ignore */} ```jsx title="myMarkdownFile.mdx" import CodeBlock from '@theme/CodeBlock'; import MyComponentSource from '!!raw-loader!./myComponent'; {MyComponentSource} ``` ```mdx-code-block import CodeBlock from '@theme/CodeBlock'; import MyComponentSource from '!!raw-loader!@site/src/pages/examples/_myComponent'; {MyComponentSource} ``` See [using code blocks in JSX](./markdown-features-code-blocks.mdx#usage-in-jsx) for more details of the `` component. :::note You have to use `` rather than the Markdown triple-backtick ` ``` `, because the latter will ship out any of its content as-is, but you want to interpolate the imported text here. ::: :::warning This feature is experimental and might be subject to breaking API changes in the future. ::: ## Importing Markdown {#importing-markdown} You can use Markdown files as components and import them elsewhere, either in Markdown files or in React pages. By convention, using the **`_` filename prefix** will not create any doc page and means the Markdown file is a **"partial"**, to be imported by other files. ```md title="_markdown-partial-example.mdx" Hello {props.name} This is text some content from `_markdown-partial-example.mdx`. ``` {/* prettier-ignore */} ```jsx title="someOtherDoc.mdx" import PartialExample from './_markdown-partial-example.mdx'; ``` ```mdx-code-block import PartialExample from './_markdown-partial-example.mdx'; ``` This way, you can reuse content among multiple pages and avoid duplicating materials. ## Available exports {#available-exports} Within the MDX page, the following variables are available as globals: - `frontMatter`: the front matter as a record of string keys and values; - `toc`: the table of contents, as a tree of headings. See also [Inline TOC](./markdown-features-toc.mdx#inline-table-of-contents) for a more concrete use-case. - `contentTitle`: the Markdown title, which is the first `h1` heading in the Markdown text. It's `undefined` if there isn't one (e.g. title specified in the front matter). ```jsx import TOCInline from '@theme/TOCInline'; import CodeBlock from '@theme/CodeBlock'; The table of contents for this page, serialized: {JSON.stringify(toc, null, 2)} The front matter of this page:

    {Object.entries(frontMatter).map(([key, value]) =>
  • {key}: {value}
  • )}

The title of this page is: {contentTitle}

``` ```mdx-code-block import TOCInline from '@theme/TOCInline'; The table of contents for this page, serialized: {JSON.stringify(toc, null, 2)} The front matter of this page:
    {Object.entries(frontMatter).map(([key, value]) =>
  • {key}: {value}
  • )}

The title of this page is: {contentTitle}

```