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

MDX is a powerful format that allows you to write JSX embedded within Markdown documents. This capability enables developers to create rich documentation, blogs, or content-heavy sites that leverage the simplicity of Markdown while incorporating React components for interactive and dynamic content. The MDX ecosystem consists of various packages, with @mdx-js/mdx focusing on the compilation of MDX files into React components, while @mdx-js/react provides the necessary components and context for rendering MDX content within a React application seamlessly.

Package Weekly Downloads Trend
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
@mdx-js/react7,093,80518,12814.4 kB94 months agoMIT
@mdx-js/mdx2,806,90518,128164 kB94 months agoMIT
Feature Comparison: @mdx-js/react vs @mdx-js/mdx

Compilation

  • @mdx-js/react:

    @mdx-js/react does not handle compilation but rather provides the runtime components and context needed to render the compiled MDX content in a React application. It allows you to use the MDX components directly in your JSX, making it easy to integrate MDX content into your existing React components.

  • @mdx-js/mdx:

    @mdx-js/mdx is responsible for compiling MDX files into React components. It transforms the MDX syntax, allowing you to write Markdown with embedded JSX, which is then converted to a format that React can render. This package is crucial for developers who want to preprocess MDX files during their build process, ensuring that the content is ready for consumption by React applications.

Integration

  • @mdx-js/react:

    @mdx-js/react is specifically tailored for use within React applications. It provides a straightforward way to render MDX content by wrapping it in a React component. This package is ideal for developers looking to directly incorporate MDX content into their React component tree without additional setup.

  • @mdx-js/mdx:

    @mdx-js/mdx is designed to work with various build tools and frameworks, making it versatile for different project setups. It can be integrated into static site generators, build systems, or any environment where you need to compile MDX files into React components before serving them.

Component Usage

  • @mdx-js/react:

    @mdx-js/react provides a set of components that allow you to render MDX content easily. It includes components like MDXProvider, which allows you to customize how MDX elements are rendered, enabling a flexible approach to styling and behavior of the content.

  • @mdx-js/mdx:

    @mdx-js/mdx does not provide components for rendering; instead, it focuses on the compilation aspect. Developers will need to use the output of this package in conjunction with @mdx-js/react or other rendering solutions to display the content in a React application.

Customization

  • @mdx-js/react:

    @mdx-js/react offers customization options through the MDXProvider, allowing developers to override default components used for rendering specific Markdown elements. This feature is particularly useful for maintaining consistent styling and behavior across different MDX files.

  • @mdx-js/mdx:

    @mdx-js/mdx allows for customization during the compilation process, enabling developers to define how specific Markdown elements are transformed into React components. This flexibility is essential for creating a tailored content experience.

Ecosystem Compatibility

  • @mdx-js/react:

    @mdx-js/react is specifically designed for React applications, ensuring seamless integration with the React ecosystem. It works well with other React libraries and tools, making it a natural choice for developers already using React for their projects.

  • @mdx-js/mdx:

    @mdx-js/mdx is compatible with various JavaScript frameworks and build tools, making it a versatile choice for developers looking to incorporate MDX into their projects. It can be used alongside tools like Webpack, Next.js, or Gatsby, providing flexibility in how MDX is integrated into applications.

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 to render MDX content directly. It provides the components and context necessary for integrating MDX seamlessly into your React components.

  • @mdx-js/mdx:

    Choose @mdx-js/mdx if you need to compile MDX files into React components and want to integrate MDX into your build process. It is essential for transforming MDX syntax into usable React elements.

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