Skip to content
Agents tracked: 440 Downloads (7d): 202M down 19.5% GitHub stars: 10.4M Releases (7d): 607 Agent status: 3 with issuesSince last limit reset: Claude 28d · Codex 0d Updated Oct 21, 2026

Custom components in react-markdown

The components prop maps tag names to your own React components, so you control how each markdown element is rendered without touching the markdown.

All react-markdown examples react-markdown: usage and stats

Custom components in react-markdown: screenshot of the example below running
The code below, running in Chromium on our server (Oct 12, 2026). Press Run to see it live and change it.

How it works

  1. Map tag names to components. The components object has keys h2, blockquote, pre and code. Each value is a function component that receives the element's props. [1]
  2. Drop the node prop. Every component also gets a node prop with the original syntax tree element. The example pulls it out before spreading the rest onto a DOM element. [1]
  3. Detect the code language. Fenced code gets a className like language-js. The code component reads it with the same regular expression the docs use and prints the language as a label. [1]

Run and edit it

It runs in your browser, in a sandboxed frame that cannot see this site; the packages load from esm.sh only after you press Run.

import Markdown from 'react-markdown'

const markdown = `## Install

> You need Node 18 or newer.

Run \`npm install\` and start the dev server:

~~~js
console.log('It works!')
~~~
`

const components = {
  h2({ node, ...rest }) {
    return <h2 style={{ margin: '0 0 10px', paddingLeft: 10, borderLeft: '4px solid #6366f1', fontSize: 20 }} {...rest} />
  },
  blockquote({ node, children }) {
    return <div style={{ background: '#fef3c7', color: '#92400e', padding: '8px 12px', borderRadius: 8 }}>⚠ {children}</div>
  },
  pre({ node, ...rest }) {
    return <pre style={{ margin: 0 }} {...rest} />
  },
  code({ node, className, children, ...rest }) {
    const match = /language-(\w+)/.exec(className || '')
    return match ? (
      <code {...rest} style={{ display: 'block', background: '#0f172a', color: '#a5f3fc', padding: 12, borderRadius: 8 }}>
        <span style={{ color: '#f472b6' }}>{match[1]} ▸ </span>{children}
      </code>
    ) : (
      <code {...rest} style={{ background: '#e0e7ff', padding: '1px 5px', borderRadius: 4 }}>{children}</code>
    )
  },
}

export default function App() {
  return (
    <div style={{ minHeight: '100vh', display: 'grid', placeItems: 'center', background: '#111', fontFamily: 'system-ui, sans-serif' }}>
      <div style={{ background: '#f8fafc', color: '#0f172a', borderRadius: 12, padding: 20, width: 440, fontSize: 14 }}>
        <Markdown components={components}>{markdown}</Markdown>
      </div>
    </div>
  )
}
Ctrl/⌘ + Enter runs

Free for signed-in members, with a daily limit. Describe the change in plain words; the new code runs only in your browser.

Change the colours in the h2 or blockquote component and the rendered markdown updates at once. Code written by AgentGid [1].

Checked Oct 12, 2026: it starts without errors in Chromium with react-markdown 10.1.0, react 19.3.0, react-dom 19.3.0. Needs WebGL.

More react-markdown examples

Rendering markdown with react-markdown Filtering elements in react-markdown Handling HTML in markdown with react-markdown Transforming link URLs in react-markdown

Sources

  1. react-markdown README (GitHub)

Explanations written by AgentGid from the official docs; the numbers in brackets point here. The code marked "written by AgentGid" is ours and free to copy.

Building something with react-markdown? Ask AgentGid →