close
  • English
  • MDX and React components

    Rspress supports MDX, a content authoring format that seamlessly combines Markdown with JSX. MDX allows you to use React components directly in your documentation, combining Markdown's concise syntax with the power of the React ecosystem, making it ideal for building interactive, component-based technical documentation.

    What is MDX

    MDX is a combination of Markdown and JSX syntax, allowing you to write and use React components directly in Markdown documents.

    We recommend using .mdx for all documentation files, which enables you to write content just like regular Markdown and also import and utilize 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 will be rendered as:

    Testing the use of MDX fragments and React components.

    This is mdx fragment.

    This is a component from tsx

    You can use built-in components provided by Rspress or install some React component libraries to enrich your documentation in .mdx files.

    Routing convention

    In the docs directory, MDX fragments or React components need to be excluded from routing through the route.exclude configuration. For convenience, files starting with "_" are excluded by default via 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 will be 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 will be rendered 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.

    It is more recommended to use .mdx files to write document content and use .tsx files to write 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.