Using React components
You can build React components directly in your MDX files using React hooks.Example
This example declares aCounter component and then uses it with <Counter />.
Importing components
To import React components in your MDX files, the component files must be located in the/snippets/ folder. Learn more about reusable snippets.
Nested imports are not supported. If a React component references other components, you must import all components directly into the parent MDX file rather than importing components within component files.
Example
This example declares aColorGenerator 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
ColorGenerator component and use it in an MDX file:
Considerations
Client-side rendering impact
Client-side rendering impact
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.
Performance best practices
Performance best practices
- Optimize dependency arrays: Include only necessary dependencies in your
useEffectdependency arrays. - Memoize complex calculations: Use
useMemooruseCallbackfor 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.