|
| 1 | +# Component Patterns |
| 2 | + |
| 3 | +## Use the `createPage` HOC |
| 4 | + |
| 5 | +The `createPage` higher-order component standardizes page patterns with built-in Suspense and page key handling: |
| 6 | + |
| 7 | +```typescript |
| 8 | +import { use } from 'react'; |
| 9 | +import { createPage } from '@/components/create-page'; |
| 10 | + |
| 11 | +// Define your view component |
| 12 | +function ProductView({ |
| 13 | + product, |
| 14 | + category |
| 15 | +}: { |
| 16 | + product: Promise<Product>; |
| 17 | + category?: Promise<Category> |
| 18 | +}) { |
| 19 | + const productData = use(product); |
| 20 | + const categoryData = category ? use(category) : null; |
| 21 | + |
| 22 | + return ( |
| 23 | + <div> |
| 24 | + <h1>{productData.name}</h1> |
| 25 | + {categoryData && <p>Category: {categoryData.name}</p>} |
| 26 | + </div> |
| 27 | + ); |
| 28 | +} |
| 29 | + |
| 30 | +// Create page with fallback |
| 31 | +const ProductPage = createPage({ |
| 32 | + component: ProductView, |
| 33 | + fallback: <ProductSkeleton /> |
| 34 | +}); |
| 35 | + |
| 36 | +export default ProductPage; |
| 37 | +``` |
| 38 | + |
| 39 | +**Benefits:** |
| 40 | + |
| 41 | +- Eliminates repetitive Suspense/Await boilerplate |
| 42 | +- Consistent loading states across pages |
| 43 | +- Built-in page key management for navigation transitions |
| 44 | +- Type-safe with full TypeScript support |
| 45 | + |
| 46 | +## shadcn/ui Components |
| 47 | + |
| 48 | +**RULES**: |
| 49 | + |
| 50 | +- ✅ Add via: `npx shadcn@latest add <component-name>` |
| 51 | +- ❌ DO NOT modify `src/components/ui/` directly |
| 52 | +- ✅ Create custom components elsewhere |
| 53 | + |
| 54 | +## Suspense Boundaries |
| 55 | + |
| 56 | +Use granular Suspense boundaries for better UX: |
| 57 | + |
| 58 | +```typescript |
| 59 | +// ✅ RECOMMENDED - Multiple Suspense boundaries |
| 60 | +export default function ProductPage({ loaderData: { product, reviews } }) { |
| 61 | + return ( |
| 62 | + <div> |
| 63 | + <Suspense fallback={<ProductHeaderSkeleton />}> |
| 64 | + <Await resolve={product}> |
| 65 | + {(data) => <ProductHeader product={data} />} |
| 66 | + </Await> |
| 67 | + </Suspense> |
| 68 | + |
| 69 | + <Suspense fallback={<ReviewsSkeleton />}> |
| 70 | + <Await resolve={reviews}> |
| 71 | + {(data) => <ProductReviews reviews={data} />} |
| 72 | + </Await> |
| 73 | + </Suspense> |
| 74 | + </div> |
| 75 | + ); |
| 76 | +} |
| 77 | + |
| 78 | +// ⚠️ OK - Single Suspense boundary (less granular) |
| 79 | +export default createPage({ |
| 80 | + component: ProductView, |
| 81 | + fallback: <ProductPageSkeleton /> |
| 82 | +}); |
| 83 | +``` |
| 84 | + |
| 85 | +## File Organization |
| 86 | + |
| 87 | +``` |
| 88 | +src/components/product-tile/ |
| 89 | +├── index.tsx # Component |
| 90 | +├── index.test.tsx # Tests |
| 91 | +└── stories/ |
| 92 | + ├── index.stories.tsx # Storybook stories |
| 93 | + └── __snapshots__/ # Storybook snapshots (optional) |
| 94 | + └── product-tile-snapshot.tsx.snap |
| 95 | +
|
| 96 | +# Skeleton components are separate components |
| 97 | +src/components/product-skeleton/ |
| 98 | +├── index.tsx |
| 99 | +├── index.test.tsx |
| 100 | +└── stories/ |
| 101 | + └── index.stories.tsx |
| 102 | +``` |
| 103 | + |
| 104 | +## Best Practices |
| 105 | + |
| 106 | +1. **Extract view components** - Separate data handling from presentation |
| 107 | +2. **Type safety** - Define proper TypeScript interfaces |
| 108 | +3. **Consistent fallbacks** - Reusable skeleton components |
| 109 | +4. **Colocate tests** - Keep tests next to components |
| 110 | +5. **Story coverage** - Create stories for all reusable components |
0 commit comments