React
Build interactive and reusable elements with React components
React components are a powerful way to create interactive and reusable elements in your documentation.
You can use React directly in your MDX files without any additional setup.
Using React Components
You can build components directly in your MDX files using React hooks.
Basic Example
Here’s a basic example of a counter component:
The Counter
component can then be used in your MDX files like this:
And the component will be rendered as a React component in the MDX file.
Importing Components
Just like in regular React, you can import components from other files.
But unlike regular React, you can’t import components from every MDX file. Re-usable components can only be referenced from MDX files within the snippets
folder.
After importing the component, you can use it in your MDX files like this:
Learn more about reusable snippets.
Complex Example
You can also build much more complex components. Here’s an example of a color generator component that uses multiple React hooks:
The above component can then be used in your MDX files like this:
And the component will be rendered as a React component in the 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