React Markdown Component

10.1.0 · active · verified Wed Apr 22

React-markdown is a robust and secure React component designed to render markdown strings into a React element tree. It is currently at version 10.1.0 and is actively maintained with regular minor and patch releases, and significant breaking changes occurring with major version bumps (e.g., v9, v10). A key differentiator is its commitment to security, rendering markdown safely without relying on `dangerouslySetInnerHTML`, thus preventing common XSS vulnerabilities by default. The component leverages the unified ecosystem, specifically remark for markdown parsing and rehype for HTML transformation, enabling extensive customization through a wide array of plugins. Developers can also provide their own React components to override default HTML element renderings, offering fine-grained control over the output. Unlike alternatives that might directly inject HTML or offer less flexibility, react-markdown builds a virtual DOM from a syntax tree, ensuring React efficiently updates only what has changed. It distinguishes itself from `react-remark` and `rehype-react` by being simpler for beginners, while MDX serves a different purpose for embedding JSX within markdown.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates basic usage of the `react-markdown` component to render a markdown string to React elements in a browser environment.

import React from 'react'
import {createRoot} from 'react-dom/client'
import Markdown from 'react-markdown'

const markdown = '# Hi, *Pluto*!\n\nThis is a simple example of how to use `react-markdown`.\n\n- It renders CommonMark by default.\n- You can pass plugins for GFM or other features.\n\nEnjoy rendering your markdown!'

const rootElement = document.getElementById('root') || document.createElement('div');
if (!document.getElementById('root')) {
  document.body.appendChild(rootElement);
}
const root = createRoot(rootElement);

root.render(<Markdown>{markdown}</Markdown>);

view raw JSON →