Interactive MDX Content
MDX lets you use JSX components directly in your Markdown content. This means you can create rich, interactive documentation and blog posts.
Embedded React Component
Here’s a fully interactive React component embedded in this MDX file:
This interactive React component is embedded directly in MDX content.
The component above is hydrated when it scrolls into view (client:visible), demonstrating Astro’s partial hydration in MDX content.
Custom Callout Components
Pro Tip
MDX components can be Astro components too! This callout is a static Astro component that ships zero JavaScript.
Warning
Remember that interactive components need a client: directive to hydrate.
Without it, they render as static HTML.
Did you know?
Astro’s MDX integration automatically handles imports and component rendering. Just import your components and use them like HTML tags.
Code with Explanation
You can mix code blocks with interactive examples:
// This is the component used above
export default function InteractiveCounter({ step = 1 }) {
const [count, setCount] = useState(0);
return (
<button onClick={() => setCount(c => c + step)}>
Count: {count}
</button>
);
}
Try changing the step value in the component above to see how props work!
Another Interactive Example
This interactive React component is embedded directly in MDX content.
This counter uses client:load to hydrate immediately when the page loads.
Benefits of MDX
- Write content naturally - Use Markdown for prose
- Add interactivity - Embed framework components where needed
- Reuse components - Same components work in pages and content
- Type safety - Full TypeScript support for component props
- Content collections - Organize and query your MDX content