Using React components
You can build React components directly in yourMDX
files using React hooks.
Example
This example declares aCounter
component and then uses it with <Counter />
.
Importing components
You can import components from yoursnippets
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 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
useEffect
dependency arrays. - Memoize complex calculations: Use
useMemo
oruseCallback
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.