@mdx-js/react vs @mdx-js/mdx
MDX Libraries for React Comparison
3 Years
@mdx-js/react@mdx-js/mdxSimilar Packages:
What's MDX Libraries for React?

MDX (Markdown for JSX) is a powerful format that allows you to write JSX (React components) directly within Markdown files. This enables a seamless blend of content and interactivity, making it ideal for documentation, blogs, and educational platforms. The MDX ecosystem provides tools to parse, compile, and render MDX files, allowing developers to create rich, component-driven content with ease. The two main packages in the MDX ecosystem are @mdx-js/mdx and @mdx-js/react, each serving distinct purposes. @mdx-js/mdx focuses on compiling MDX files into static React components, while @mdx-js/react provides the necessary context and components to render MDX content within a React application. Together, they enable developers to harness the full potential of MDX, creating dynamic and interactive content that enhances user engagement.

Package Weekly Downloads Trend
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
@mdx-js/react7,741,881
18,74714.4 kB1610 months agoMIT
@mdx-js/mdx2,892,172
18,747164 kB1610 months agoMIT
Feature Comparison: @mdx-js/react vs @mdx-js/mdx

Compilation vs Rendering

  • @mdx-js/react:

    @mdx-js/react provides the runtime environment for rendering MDX content within React applications. It allows for dynamic rendering of MDX files, supporting interactive components and real-time updates.

  • @mdx-js/mdx:

    @mdx-js/mdx is primarily focused on compiling MDX files into React components. It transforms MDX content during the build process, allowing for static generation of pages with embedded JSX.

Integration with Build Tools

  • @mdx-js/react:

    @mdx-js/react is designed to work within React applications and does not handle file processing. It relies on the MDX content being pre-compiled or provided as a prop, making it complementary to build tools.

  • @mdx-js/mdx:

    @mdx-js/mdx integrates seamlessly with popular build tools like Webpack, Rollup, and Vite. It can be configured to process MDX files alongside other assets, making it versatile for various project setups.

Custom Component Support

  • @mdx-js/react:

    @mdx-js/react excels in rendering MDX content with custom components. It provides a <MDXProvider> component that allows you to easily override default HTML elements and integrate custom React components.

  • @mdx-js/mdx:

    @mdx-js/mdx allows for the use of custom components within MDX files, but the integration is primarily during the compilation phase. You need to define your components and pass them to the compiler.

Static vs Dynamic Content

  • @mdx-js/react:

    @mdx-js/react supports both static and dynamic content. It can render MDX files that include interactive components, making it suitable for applications that require real-time interactivity.

  • @mdx-js/mdx:

    @mdx-js/mdx is ideal for static content generation. Once compiled, the MDX files produce static React components that can be rendered without any additional processing.

Ease of Use: Code Examples

  • @mdx-js/react:

    MDX Rendering Example

    import { MDXProvider } from '@mdx-js/react';
    import MyCustomComponent from './MyCustomComponent';
    import mdxContent from './content.mdx';
    
    const components = {
      h1: (props) => <h1 style={{ color: 'blue' }} {...props} />, // Override h1
      CustomComponent: MyCustomComponent, // Custom component
    };
    
    <MDXProvider components={components}>
      {mdxContent}
    </MDXProvider>;
    
  • @mdx-js/mdx:

    MDX Compilation Example

    import { mdx } from '@mdx-js/react';
    import { compile } from '@mdx-js/mdx';
    
    const mdxSource = `# Hello, MDX\n\n<CustomComponent />`;
    
    const compiled = await compile(mdxSource);
    const Component = mdx(compiled);
    
    // Render the compiled MDX component
    <Component />;
    
How to Choose: @mdx-js/react vs @mdx-js/mdx
  • @mdx-js/react:

    Choose @mdx-js/react if you are building a React application and need a library that provides the context and components to render MDX content dynamically. It is perfect for applications that require real-time rendering of MDX with support for custom components.

  • @mdx-js/mdx:

    Choose @mdx-js/mdx if you need a standalone compiler for MDX files that integrates with your build process. It is ideal for generating static sites or applications where you want to pre-render MDX content into React components.

README for @mdx-js/react

@mdx-js/react

Build Coverage Downloads Size Sponsors Backers Chat

React context for MDX.

Contents

What is this?

This package is a context based components provider for combining React with MDX.

When should I use this?

This package is not needed for MDX to work with React. See ¶ MDX provider in § Using MDX for when and how to use an MDX provider.

If you use Next.js, do not use this. Add an mdx-components.tsx (in src/ or /) file instead. See Configuring MDX on nextjs.org for more info.

Install

This package is ESM only. In Node.js (version 16+), install with npm:

npm install @mdx-js/react

In Deno with esm.sh:

import {MDXProvider} from 'https://esm.sh/@mdx-js/react@3'

In browsers with esm.sh:

<script type="module">
  import {MDXProvider} from 'https://esm.sh/@mdx-js/react@3?bundle'
</script>

Use

/**
 * @import {MDXComponents} from 'mdx/types.js'
 */

import {MDXProvider} from '@mdx-js/react'
import Post from './post.mdx'
// ^-- Assumes an integration is used to compile MDX to JS, such as
// `@mdx-js/esbuild`, `@mdx-js/loader`, `@mdx-js/node-loader`, or
// `@mdx-js/rollup`, and that it is configured with
// `options.providerImportSource: '@mdx-js/react'`.

/** @type {MDXComponents} */
const components = {
  em(properties) {
    return <i {...properties} />
  }
}

console.log(
  <MDXProvider components={components}>
    <Post />
  </MDXProvider>
)

👉 Note: you don’t have to use MDXProvider and can pass components directly:

-<MDXProvider components={components}>
-  <Post />
-</MDXProvider>
+<Post components={components} />

See ¶ React in § Getting started for how to get started with MDX and React. See ¶ MDX provider in § Using MDX for how to use an MDX provider.

API

This package exports the identifiers MDXProvider and useMDXComponents. There is no default export.

MDXProvider(properties?)

Provider for MDX context.

Parameters
  • properties (Props) — configuration
Returns

Element (JSX.Element).

useMDXComponents(components?)

Get current components from the MDX Context.

Parameters
Returns

Current components (MDXComponents from mdx/types.js).

MergeComponents

Custom merge function (TypeScript type).

Parameters
Returns

Additional components (MDXComponents from mdx/types.js).

Props

Configuration for MDXProvider (TypeScript type).

Fields

Types

This package is fully typed with TypeScript. It exports the additional types MergeComponents and Props.

For types to work, make sure the TypeScript JSX namespace is typed. This is done by installing and using the types of your framework, as in @types/react.

Compatibility

Projects maintained by the unified collective are compatible with maintained versions of Node.js.

When we cut a new major release, we drop support for unmaintained versions of Node. This means we try to keep the current release line, @mdx-js/react@^3, compatible with Node.js 16.

Security

See § Security on our website for information.

Contribute

See § Contribute on our website for ways to get started. See § Support for ways to get help.

This project has a code of conduct. By interacting with this repository, organization, or community you agree to abide by its terms.

License

MIT © Compositor and Vercel