Originally published with interactive examples at lukiluki.me.
You are writing technical documentation in Markdown. Plain text works until the reader needs a calculator, a live example or a reusable warning box. Copying HTML fragments across articles makes them harder to maintain. How can a document stay readable while using the same components as the application?
MDX combines Markdown with JSX, so documents can import and render React components. In the Next.js App Router, local MDX can participate in server rendering while interactive components live behind explicit client boundaries. This makes shared styling and focused interaction possible. The result still depends on the imported components: using MDX alone does not guarantee static output, type-safe metadata or zero client JavaScript.
Merksatz (Key takeaway):
Use Markdown for the learning path and components for tasks that need them. Keep interactive state in separate client components and check what reaches the browser.
How MDX works in the App Router
The MDX compiler turns a document into JavaScript that renders React elements. Headings and paragraphs become elements; imported components appear where they are used in the document. The MDX format guide explains the combination of Markdown, JSX, expressions and module imports.
With @next/mdx, Next.js processes local .mdx files through its build pipeline. An RSC (React Server Component: A component evaluated on the server whose rendered output can be composed with interactive Client Components.) can render the document and include client components where needed. Client components still need their JavaScript and hydration; importing one does not make the entire surrounding article a client component.
| Document element | Role | What to verify |
|---|---|---|
| Markdown headings, paragraphs and lists | Structure and readable explanations | The rendered heading hierarchy and semantics |
| Shared component mapping | Consistent design-system presentation | Mapping does not introduce unnecessary client dependencies |
| Imported client component | State, event handlers or browser APIs | The interaction works and has accessible controls |
| Metadata and frontmatter | Search, page titles and exports | The project explicitly parses and validates the fields |
Map standard elements to the design system
The App Router uses mdx-components.tsx to define component mappings. This allows a Markdown code fence or heading to reuse a styled application component. Next.js documents this setup, including the required mapping file and local MDX imports.
For example, a shared code component can provide syntax highlighting, a frame and a copy button for every article. Keeping that behavior in one place is easier to maintain than repeating styled HTML snippets. It also means the document inherits the behavior and client requirements of that shared component.
Frontmatter (Metadata stored at the start of a document, often as YAML between delimiter lines; parsing and validation must be configured by the application.) is not automatically a validated schema. In this garden, a build script parses metadata and checks it with Zod before generating the index. That is project infrastructure on top of MDX.
Keep interaction in a separate component
Before using this pattern, configure @next/mdx and the component mapping for your Next.js project. The example assumes an App Router application with an @/ source alias. The component file is ordinary TSX; the article imports it rather than defining hooks inside the MDX document.
"use client"; import { useState } from "react"; export default function Counter() { const [count, setCount] = useState(0); return ( <button type="button" onClick={() => setCount((value) => value + 1)} className="min-h-11 rounded-lg border px-4 py-2" > Clicks: {count} </button> ); } Save that component as src/components/Counter.tsx. The corresponding article can then contain:
import Counter from "@/components/Counter"; ## Try the example Activate the button by mouse or keyboard and watch the count change. <Counter /> The expected result is a heading, an explanatory paragraph and a button whose count changes. Build the page, then test the interaction in the browser. A successful MDX compilation alone does not verify keyboard behavior, layout stability or the correctness of a larger interactive tool.
Use components where they improve the document
Markdown is a good default for explanations, lists and comparison tables. A reusable callout helps when it gives a warning a clear role. An interactive tool is useful when inputs change real data or behavior. A collection of preset comparisons can usually remain a table.
Keep large dependencies behind deliberate boundaries and load them when the reader needs them. Avoid placing client hooks directly in an article or importing an entire component catalog for one small example. Review the resulting bundle rather than assuming an island is free.
One design system, multiple reading depths
Shared components can keep documentation consistent with the application. UI Components discusses component libraries, while Explainable Text shows how optional explanations can stay in the reading flow.
MDX checklist
- [ ] Put state, event handlers and browser APIs in separate client files.
- [ ] Keep the page's H1 in its wrapper and use a coherent H2/H3 structure in the article.
- [ ] Parse metadata explicitly and validate it against the page configuration.
- [ ] Build the application, then test any imported interaction in a real browser.
- [ ] Inspect the text export so important explanations survive without JSX.
Sources and next steps
- MDX: What is MDX? – syntax, imports and embedded components.
- Next.js: MDX guide – local integration and component mapping; check the guide for your installed version.
For shared UI choices, continue with UI Components. For optional depth within a paragraph, see Explainable Text.
Read the original guide & explore the interactive playground on *lukiluki.me*.
