Skip to main content
React components are a powerful way to create interactive and reusable elements in your documentation.

Using React components

You can build React components directly in your MDX files using React hooks.

Example

This example declares a Counter component and then uses it with <Counter />.
The counter renders as an interactive React component.

Importing components

You can import components from your snippets folder. Unlike regular React, you cannot import components from every MDX file. Reusable components must be referenced from files within the snippets folder. Learn more about reusable snippets.

Example

This example declares a ColorGenerator component that uses multiple React hooks and then uses it in an MDX file. Create color-generator.jsx file in the snippets folder:
/snippets/color-generator.jsx
Import the ColorGenerator component and use it in an MDX file:
The color generator renders as an interactive React component.

Considerations

React hook components render on the client-side, which has several implications:
  • SEO: Search engines might not fully index dynamic content.
  • Initial load: Visitors may experience a flash of loading content before components render.
  • Accessibility: Ensure dynamic content changes are announced to screen readers.
  • Optimize dependency arrays: Include only necessary dependencies in your useEffect dependency arrays.
  • Memoize complex calculations: Use useMemo or useCallback for expensive operations.
  • Reduce re-renders: Break large components into smaller ones to prevent cascading re-renders.
  • Lazy loading: Consider lazy loading complex components to improve initial page load time.