Skip to content

Commit 897ea8d

Browse files
committed
chore: add loadable.tsx to react-component skill
1 parent 4b80b35 commit 897ea8d

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

‎skills/react-component/SKILL.md‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ Use the `ui-component` skill as the base — it holds the framework-agnostic HTM
5656
[ComponentName].stories.tsx — stories (optional, if Storybook is used)
5757
[ComponentName].spec.tsx — tests (optional, on request)
5858
use[HookName].ts — component logic extracted into a hook (optional, for complex components with a lot of logic)
59+
loadable.tsx — lazy-loaded wrapper (optional, when the component pulls in a heavy dependency)
5960
```
6061

6162
## Reference component
@@ -133,6 +134,41 @@ export const Primary: Story = {
133134
}
134135
```
135136

137+
## Loadable components
138+
139+
When a component pulls in a heavy dependency (a rich-text editor, a chart library, a syntax highlighter etc.), add a `loadable.tsx` next to it — a wrapper that loads the component lazily via `lazy` + `Suspense`, so the dependency stays out of the main bundle:
140+
141+
```tsx
142+
// loadable.tsx
143+
import {
144+
type ReactNode,
145+
Suspense,
146+
lazy
147+
} from 'react'
148+
import type { IMDXEditorProps } from './MDXEditor'
149+
150+
export interface ILoadableMDXEditorProps extends IMDXEditorProps {
151+
fallback?: ReactNode
152+
}
153+
154+
const LazyMDXEditor = lazy(() => import('./MDXEditor').then(({ MDXEditor }) => ({
155+
default: MDXEditor
156+
})))
157+
158+
export function MDXEditor({
159+
fallback,
160+
...props
161+
}: ILoadableMDXEditorProps) {
162+
return (
163+
<Suspense fallback={fallback}>
164+
<LazyMDXEditor {...props} />
165+
</Suspense>
166+
)
167+
}
168+
```
169+
170+
The wrapper keeps the original component name and props, adding only an optional `fallback` prop for the `Suspense` fallback.
171+
136172
## Rules
137173

138174
- Code that doesn't directly depend on the component must be moved out of the component body, to module scope:

0 commit comments

Comments
 (0)