close
  • English
  • MDX and React components

    Rspress supports MDX, a content authoring format that seamlessly combines Markdown with JSX. MDX lets you use React components directly in your documentation, pairing Markdown's concise syntax with the React ecosystem. It is ideal for building interactive, component-based technical documentation.

    What is MDX

    MDX combines Markdown and JSX syntax, so you can write Markdown content and use React components in the same file.

    We recommend using .mdx for all documentation files. This lets you write content like regular Markdown while importing and using the built-in components provided by Rspress.

    docs/index.mdx
    # Hello, world!
    
    import { PackageManagerTabs } from '@rspress/core/theme';
    
    <PackageManagerTabs command="create rspress@latest" />

    MDX fragments

    In MDX, every .mdx file is compiled into a React component, which means it can be imported like any component and can freely render React components. For example:

    docs/index.mdx
    docs/_mdx-fragment.mdx
    docs/_tsx-component.tsx
    import MdxFragment from './_mdx-fragment.mdx';
    import TsxComponent from './_tsx-component';
    
    Testing the use of MDX fragments and React components.
    
    <MdxFragment />
    
    <TsxComponent />

    It renders as:

    Testing the use of MDX fragments and React components.

    This is mdx fragment.

    This is a component from tsx

    In .mdx files, you can use the built-in components provided by Rspress or install React component libraries to enrich your documentation.

    Routing convention

    In the docs directory, MDX fragments or React components must be excluded from routing with route.exclude. For convenience, files starting with "_" are excluded by default through route.excludeConvention.

    You can also place components in adjacent directories outside the docs directory. For example:

    docs
    _button.mdx
    index.mdx
    components
    button.tsx
    docs/index.mdx
    docs/_button.mdx
    components/button.tsx
    import ButtonFragment from './_button.mdx';
    import Button from '../../components/button';
    
    <ButtonFragment />
    <Button />

    It is rendered as:

    button

    This is text from MDX

    Escape Hatch: writing document content in tsx

    _escape-hatch.tsx
    import { getCustomMDXComponent } from '@rspress/core/theme';
    
    export default () => {
      const { p: P, code: Code } = getCustomMDXComponent();
      return (
        <P className="rp-doc">
          This is content in tsx, but the styles are the same as in the
          documentation, such as <Code>@rspress/core</Code>. However, this text with
          className="rp-not-doc"
          <Code className="rp-not-doc">@rspress/core</Code> will not take effect
        </P>
      );
    };
    

    It renders as:

    This is content in tsx, but the styles are the same as in the documentation, such as @rspress/core. However, this text with className="rp-not-doc"@rspress/core will not take effect

    Warning

    TSX and HTML syntax can make it difficult to extract static information, such as local search indexes.

    We recommend using .mdx files for document content and .tsx files for interactive dynamic content.

    React version requirements

    DependencyAllowed RangeDefault VersionNotes
    react^18.0.0 || ^19.0.019React 17 is no longer supported
    react-dom^18.0.0 || ^19.0.019Keep consistent with react version
    react-router-dom^6.0.0 || ^7.0.07Uses project version if already installed
    Tip

    If react, react-dom, or react-router-dom is already installed in your project, Rspress will prioritize using the version installed in your project rather than the built-in default version.