React components in Docfiy run in a sandboxed MDX environment with the following constraints:
React hooks are pre-injected: useState, useEffect, useRef, useCallback, useMemo, useContext, and useReducer are available without importing them.
No external npm packages: Third-party packages (for example, lodash, axios, date-fns) cannot be imported. Use browser built-ins or write the logic inline.
No default exports: Use named exports (export const MyComponent = ...). Default exports (export default) are not supported.
No cross-snippet imports: Snippet files cannot import other snippet files. Import all dependencies directly in the parent MDX file.
No JSON imports: Importing .json files is not supported.
Import components
Component files must be in the /snippets/ folder. Learn more about reusable snippets.
Nested imports are not supported. Import all referenced components directly into the parent MDX file.
Optimize dependency arrays: Only include necessary dependencies in useEffect.
Memoize expensive operations: Use useMemo or useCallback where appropriate.
Reduce re-renders: Break large components into smaller ones.
Lazy loading: Defer rendering complex components to improve initial page load. Because the MDX sandbox does not support React.lazy or dynamic import(), gate heavy components behind user interaction or visibility instead. See Defer rendering for heavy components.
Defer rendering for heavy components
React.lazy, Suspense, and dynamic import() are not available in the MDX sandbox. To get the same benefit, render a lightweight placeholder first and only mount the expensive component after the reader opts in. This keeps the initial page load fast while still letting readers interact with the full component.
The example below keeps the ColorGenerator from the previous section unmounted until the reader clicks Load color generator:
To defer rendering until the component scrolls into view, swap the button for an IntersectionObserver set up inside a useEffect. The pattern is the same: keep show false until the trigger fires, then return the heavy component.
Was this page helpful?
⌘I
Assistant
Responses are generated using AI and may contain mistakes.