@ruyfranca/myskills 1.0.28 → 1.0.30
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.agent/rules/AGENTS.md +273 -0
- package/.agent/skills/cqrs-implementation/SKILL.md +107 -0
- package/.agent/skills/ddd-strategic-design/SKILL.md +70 -0
- package/.agent/skills/ddd-tactical-patterns/SKILL.md +70 -0
- package/.agent/skills/elixir-pro/SKILL.md +89 -0
- package/.agent/skills/event-sourcing-architect/SKILL.md +66 -0
- package/.agent/skills/golang-pro/SKILL.md +121 -0
- package/.agent/skills/kotlin-coroutines-expert/SKILL.md +99 -0
- package/.agent/skills/modern-javascript-patterns/SKILL.md +131 -0
- package/.agent/skills/monorepo-architect/SKILL.md +91 -0
- package/.agent/skills/nextjs-react-expert/SKILL.md +94 -247
- package/.agent/skills/react-patterns/SKILL.md +200 -0
- package/.agent/skills/react-state-management/SKILL.md +147 -0
- package/.agent/skills/ruby-pro/SKILL.md +105 -0
- package/.agent/skills/zod-validation-expert/SKILL.md +132 -0
- package/AGENTS.md +273 -0
- package/index.js +42 -9
- package/package.json +1 -1
|
@@ -1,286 +1,133 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: react-best-practices
|
|
3
|
-
description: React and Next.js performance optimization from Vercel Engineering. Use when building React components, optimizing performance, eliminating waterfalls, reducing bundle size, reviewing code for performance issues, or implementing server/client-side optimizations.
|
|
4
|
-
|
|
3
|
+
description: "React and Next.js performance optimization from Vercel Engineering. Use when building React components, optimizing performance, eliminating waterfalls, reducing bundle size, reviewing code for performance issues, or implementing server/client-side optimizations."
|
|
4
|
+
risk: unknown
|
|
5
|
+
source: community
|
|
6
|
+
date_added: "2026-02-27"
|
|
5
7
|
---
|
|
6
8
|
|
|
7
|
-
#
|
|
9
|
+
# React Best Practices (Vercel)
|
|
8
10
|
|
|
9
|
-
|
|
10
|
-
> **Philosophy:** Eliminate waterfalls first, optimize bundles second, then micro-optimize.
|
|
11
|
+
Comprehensive performance optimization guide for React and Next.js applications, maintained by Vercel. Contains rules across 8 categories, prioritized by impact.
|
|
11
12
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
## 🎯 Selective Reading Rule (MANDATORY)
|
|
15
|
-
|
|
16
|
-
**Read ONLY sections relevant to your task!** Check the content map below and load what you need.
|
|
17
|
-
|
|
18
|
-
> 🔴 **For performance reviews: Start with CRITICAL sections (1-2), then move to HIGH/MEDIUM.**
|
|
19
|
-
|
|
20
|
-
---
|
|
13
|
+
## Use this skill when
|
|
21
14
|
|
|
22
|
-
|
|
15
|
+
- Building React components with Next.js App Router
|
|
16
|
+
- Optimizing performance or eliminating waterfalls
|
|
17
|
+
- Reducing bundle size or improving Core Web Vitals
|
|
18
|
+
- Reviewing code for performance issues
|
|
23
19
|
|
|
24
|
-
|
|
25
|
-
| --------------------------------------- | ------------------ | -------- | --------------------------------------------------------------- |
|
|
26
|
-
| `1-async-eliminating-waterfalls.md` | 🔴 **CRITICAL** | 5 rules | Slow page loads, sequential API calls, data fetching waterfalls |
|
|
27
|
-
| `2-bundle-bundle-size-optimization.md` | 🔴 **CRITICAL** | 5 rules | Large bundle size, slow Time to Interactive, First Load issues |
|
|
28
|
-
| `3-server-server-side-performance.md` | 🟠 **HIGH** | 7 rules | Slow SSR, API route optimization, server-side waterfalls |
|
|
29
|
-
| `4-client-client-side-data-fetching.md` | 🟡 **MEDIUM-HIGH** | 4 rules | Client data management, SWR patterns, deduplication |
|
|
30
|
-
| `5-rerender-re-render-optimization.md` | 🟡 **MEDIUM** | 12 rules | Excessive re-renders, React performance, memoization |
|
|
31
|
-
| `6-rendering-rendering-performance.md` | 🟡 **MEDIUM** | 9 rules | Rendering bottlenecks, virtualization, image optimization |
|
|
32
|
-
| `7-js-javascript-performance.md` | ⚪ **LOW-MEDIUM** | 12 rules | Micro-optimizations, caching, loop performance |
|
|
33
|
-
| `8-advanced-advanced-patterns.md` | 🔵 **VARIABLE** | 3 rules | Advanced React patterns, useLatest, init-once |
|
|
20
|
+
## Do not use this skill when
|
|
34
21
|
|
|
35
|
-
|
|
22
|
+
- The task is unrelated to React/Next.js
|
|
23
|
+
- You need basic React syntax explanations
|
|
36
24
|
|
|
37
|
-
|
|
25
|
+
## Rule Categories by Priority
|
|
38
26
|
|
|
39
|
-
|
|
27
|
+
### 1. Eliminating Waterfalls (CRITICAL)
|
|
40
28
|
|
|
41
|
-
|
|
29
|
+
```tsx
|
|
30
|
+
// ❌ Sequential — waterfall
|
|
31
|
+
const user = await getUser();
|
|
32
|
+
const posts = await getPosts(user.id); // waits for user
|
|
42
33
|
|
|
34
|
+
// ✅ Parallel — no waterfall
|
|
35
|
+
const [user, posts] = await Promise.all([getUser(), getPosts()]);
|
|
43
36
|
```
|
|
44
|
-
🐌 Slow page loads / Long Time to Interactive
|
|
45
|
-
→ Read Section 1: Eliminating Waterfalls
|
|
46
|
-
→ Read Section 2: Bundle Size Optimization
|
|
47
|
-
|
|
48
|
-
📦 Large bundle size (> 200KB)
|
|
49
|
-
→ Read Section 2: Bundle Size Optimization
|
|
50
|
-
→ Check: Dynamic imports, barrel imports, tree-shaking
|
|
51
37
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
38
|
+
**Key rules:**
|
|
39
|
+
- Fetch all parallel data with `Promise.all`
|
|
40
|
+
- Move data fetching to the closest Server Component
|
|
41
|
+
- Avoid client-side fetch when SSR/RSC is available
|
|
55
42
|
|
|
56
|
-
|
|
57
|
-
→ Read Section 5: Re-render Optimization
|
|
58
|
-
→ Check: React.memo, useMemo, useCallback
|
|
43
|
+
### 2. Bundle Size Optimization (CRITICAL)
|
|
59
44
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
45
|
+
```tsx
|
|
46
|
+
// ❌ Imports everything
|
|
47
|
+
import _ from 'lodash';
|
|
63
48
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
→ Check: SWR deduplication, localStorage
|
|
49
|
+
// ✅ Tree-shakeable import
|
|
50
|
+
import { debounce } from 'lodash-es';
|
|
67
51
|
|
|
68
|
-
|
|
69
|
-
|
|
52
|
+
// ✅ Dynamic import for heavy components
|
|
53
|
+
const Chart = dynamic(() => import('./Chart'), { ssr: false });
|
|
70
54
|
```
|
|
71
55
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
## 📊 Impact Priority Guide
|
|
56
|
+
### 3. Server-Side Performance (HIGH)
|
|
75
57
|
|
|
76
|
-
|
|
58
|
+
```tsx
|
|
59
|
+
// ✅ Server Component — no client JS
|
|
60
|
+
export default async function UserProfile({ id }: { id: string }) {
|
|
61
|
+
const user = await db.user.findUnique({ where: { id } });
|
|
62
|
+
return <div>{user.name}</div>;
|
|
63
|
+
}
|
|
77
64
|
|
|
65
|
+
// ✅ Streaming with Suspense
|
|
66
|
+
<Suspense fallback={<Skeleton />}>
|
|
67
|
+
<SlowComponent />
|
|
68
|
+
</Suspense>
|
|
78
69
|
```
|
|
79
|
-
1️⃣ CRITICAL (Biggest Gains - Do First):
|
|
80
|
-
├─ Section 1: Eliminating Waterfalls
|
|
81
|
-
│ └─ Each waterfall adds full network latency (100-500ms+)
|
|
82
|
-
└─ Section 2: Bundle Size Optimization
|
|
83
|
-
└─ Affects Time to Interactive and Largest Contentful Paint
|
|
84
|
-
|
|
85
|
-
2️⃣ HIGH (Significant Impact - Do Second):
|
|
86
|
-
└─ Section 3: Server-Side Performance
|
|
87
|
-
└─ Eliminates server-side waterfalls, faster response times
|
|
88
|
-
|
|
89
|
-
3️⃣ MEDIUM (Moderate Gains - Do Third):
|
|
90
|
-
├─ Section 4: Client-Side Data Fetching
|
|
91
|
-
├─ Section 5: Re-render Optimization
|
|
92
|
-
└─ Section 6: Rendering Performance
|
|
93
|
-
|
|
94
|
-
4️⃣ LOW (Polish - Do Last):
|
|
95
|
-
├─ Section 7: JavaScript Performance
|
|
96
|
-
└─ Section 8: Advanced Patterns
|
|
97
|
-
```
|
|
98
|
-
|
|
99
|
-
---
|
|
100
|
-
|
|
101
|
-
## 🔗 Related Skills
|
|
102
|
-
|
|
103
|
-
| Need | Skill |
|
|
104
|
-
| ----------------------- | --------------------------------- |
|
|
105
|
-
| API design patterns | `@[skills/api-patterns]` |
|
|
106
|
-
| Database optimization | `@[skills/database-design]` |
|
|
107
|
-
| Testing strategies | `@[skills/testing-patterns]` |
|
|
108
|
-
| UI/UX design principles | `@[skills/frontend-design]` |
|
|
109
|
-
| TypeScript patterns | `@[skills/typescript-expert]` |
|
|
110
|
-
| Deployment & DevOps | `@[skills/deployment-procedures]` |
|
|
111
|
-
|
|
112
|
-
---
|
|
113
|
-
|
|
114
|
-
## ✅ Performance Review Checklist
|
|
115
|
-
|
|
116
|
-
Before shipping to production:
|
|
117
|
-
|
|
118
|
-
**Critical (Must Fix):**
|
|
119
|
-
|
|
120
|
-
- [ ] No sequential data fetching (waterfalls eliminated)
|
|
121
|
-
- [ ] Bundle size < 200KB for main bundle
|
|
122
|
-
- [ ] No barrel imports in app code
|
|
123
|
-
- [ ] Dynamic imports used for large components
|
|
124
|
-
- [ ] Parallel data fetching where possible
|
|
125
|
-
|
|
126
|
-
**High Priority:**
|
|
127
|
-
|
|
128
|
-
- [ ] Server components used where appropriate
|
|
129
|
-
- [ ] API routes optimized (no N+1 queries)
|
|
130
|
-
- [ ] Suspense boundaries for data fetching
|
|
131
|
-
- [ ] Static generation used where possible
|
|
132
|
-
|
|
133
|
-
**Medium Priority:**
|
|
134
|
-
|
|
135
|
-
- [ ] Expensive computations memoized
|
|
136
|
-
- [ ] List rendering virtualized (if > 100 items)
|
|
137
|
-
- [ ] Images optimized with next/image
|
|
138
|
-
- [ ] No unnecessary re-renders
|
|
139
|
-
|
|
140
|
-
**Low Priority (Polish):**
|
|
141
|
-
|
|
142
|
-
- [ ] Hot path loops optimized
|
|
143
|
-
- [ ] RegExp patterns hoisted
|
|
144
|
-
- [ ] Property access cached in loops
|
|
145
|
-
|
|
146
|
-
---
|
|
147
|
-
|
|
148
|
-
## ❌ Anti-Patterns (Common Mistakes)
|
|
149
|
-
|
|
150
|
-
**DON'T:**
|
|
151
|
-
|
|
152
|
-
- ❌ Use sequential `await` for independent operations
|
|
153
|
-
- ❌ Import entire libraries when you need one function
|
|
154
|
-
- ❌ Use barrel exports (`index.ts` re-exports) in app code
|
|
155
|
-
- ❌ Skip dynamic imports for large components/libraries
|
|
156
|
-
- ❌ Fetch data in useEffect without deduplication
|
|
157
|
-
- ❌ Forget to memoize expensive computations
|
|
158
|
-
- ❌ Use client components when server components work
|
|
159
|
-
|
|
160
|
-
**DO:**
|
|
161
|
-
|
|
162
|
-
- ✅ Fetch data in parallel with `Promise.all()`
|
|
163
|
-
- ✅ Use dynamic imports: `const Comp = dynamic(() => import('./Heavy'))`
|
|
164
|
-
- ✅ Import directly: `import { specific } from 'library/specific'`
|
|
165
|
-
- ✅ Use Suspense boundaries for better UX
|
|
166
|
-
- ✅ Leverage React Server Components
|
|
167
|
-
- ✅ Measure performance before optimizing
|
|
168
|
-
- ✅ Use Next.js built-in optimizations (next/image, next/font)
|
|
169
|
-
|
|
170
|
-
---
|
|
171
|
-
|
|
172
|
-
## 🎯 How to Use This Skill
|
|
173
70
|
|
|
174
|
-
###
|
|
71
|
+
### 4. Client-Side Data Fetching (MEDIUM-HIGH)
|
|
175
72
|
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
2. Then **Section 2** (bundle size)
|
|
184
|
-
3. Then **Section 3** (server-side)
|
|
185
|
-
4. Finally other sections as needed
|
|
186
|
-
|
|
187
|
-
### For Debugging Slow Performance:
|
|
188
|
-
|
|
189
|
-
1. Identify the symptom (slow load, lag, etc.)
|
|
190
|
-
2. Use Quick Decision Tree above
|
|
191
|
-
3. Read relevant section
|
|
192
|
-
4. Apply fixes in priority order
|
|
193
|
-
|
|
194
|
-
---
|
|
195
|
-
|
|
196
|
-
## 📚 Learning Path
|
|
197
|
-
|
|
198
|
-
**Beginner (Focus on Critical):**
|
|
199
|
-
→ Section 1: Eliminating Waterfalls
|
|
200
|
-
→ Section 2: Bundle Size Optimization
|
|
201
|
-
|
|
202
|
-
**Intermediate (Add High Priority):**
|
|
203
|
-
→ Section 3: Server-Side Performance
|
|
204
|
-
→ Section 5: Re-render Optimization
|
|
205
|
-
|
|
206
|
-
**Advanced (Full Optimization):**
|
|
207
|
-
→ All sections + Section 8: Advanced Patterns
|
|
208
|
-
|
|
209
|
-
---
|
|
210
|
-
|
|
211
|
-
## 🔍 Validation Script
|
|
212
|
-
|
|
213
|
-
| Script | Purpose | Command |
|
|
214
|
-
| -------------------------------------- | --------------------------- | ------------------------------------------------------------ |
|
|
215
|
-
| `scripts/react_performance_checker.py` | Automated performance audit | `python scripts/react_performance_checker.py <project_path>` |
|
|
216
|
-
|
|
217
|
-
---
|
|
218
|
-
|
|
219
|
-
## 📖 Section Details
|
|
220
|
-
|
|
221
|
-
### Section 1: Eliminating Waterfalls (CRITICAL)
|
|
222
|
-
|
|
223
|
-
**Impact:** Each waterfall adds 100-500ms+ latency
|
|
224
|
-
**Key Concepts:** Parallel fetching, Promise.all(), Suspense boundaries, preloading
|
|
225
|
-
|
|
226
|
-
### Section 2: Bundle Size Optimization (CRITICAL)
|
|
227
|
-
|
|
228
|
-
**Impact:** Directly affects Time to Interactive, Largest Contentful Paint
|
|
229
|
-
**Key Concepts:** Dynamic imports, tree-shaking, barrel import avoidance
|
|
230
|
-
|
|
231
|
-
### Section 3: Server-Side Performance (HIGH)
|
|
232
|
-
|
|
233
|
-
**Impact:** Faster server responses, better SEO
|
|
234
|
-
**Key Concepts:** Parallel server fetching, streaming, API route optimization
|
|
235
|
-
|
|
236
|
-
### Section 4: Client-Side Data Fetching (MEDIUM-HIGH)
|
|
237
|
-
|
|
238
|
-
**Impact:** Reduces redundant requests, better UX
|
|
239
|
-
**Key Concepts:** SWR deduplication, localStorage caching, event listeners
|
|
240
|
-
|
|
241
|
-
### Section 5: Re-render Optimization (MEDIUM)
|
|
242
|
-
|
|
243
|
-
**Impact:** Smoother UI, less wasted computation
|
|
244
|
-
**Key Concepts:** React.memo, useMemo, useCallback, component structure
|
|
245
|
-
|
|
246
|
-
### Section 6: Rendering Performance (MEDIUM)
|
|
73
|
+
```tsx
|
|
74
|
+
// ✅ SWR for client-side data
|
|
75
|
+
const { data, isLoading } = useSWR('/api/user', fetcher, {
|
|
76
|
+
revalidateOnFocus: false,
|
|
77
|
+
dedupingInterval: 60000,
|
|
78
|
+
});
|
|
79
|
+
```
|
|
247
80
|
|
|
248
|
-
|
|
249
|
-
**Key Concepts:** Virtualization, image optimization, layout thrashing
|
|
81
|
+
### 5. Re-render Optimization (MEDIUM)
|
|
250
82
|
|
|
251
|
-
|
|
83
|
+
```tsx
|
|
84
|
+
// ✅ Memoize only when profiler shows it helps
|
|
85
|
+
const MemoComponent = memo(Component, (prev, next) =>
|
|
86
|
+
prev.id === next.id
|
|
87
|
+
);
|
|
252
88
|
|
|
253
|
-
|
|
254
|
-
|
|
89
|
+
// ✅ Stable selectors with Zustand
|
|
90
|
+
const user = useStore(state => state.user); // not state => ({ user: state.user })
|
|
91
|
+
```
|
|
255
92
|
|
|
256
|
-
###
|
|
93
|
+
### 6. Rendering Performance (MEDIUM)
|
|
257
94
|
|
|
258
|
-
|
|
259
|
-
|
|
95
|
+
```tsx
|
|
96
|
+
// ✅ Virtualize long lists
|
|
97
|
+
import { FixedSizeList as List } from 'react-window';
|
|
260
98
|
|
|
261
|
-
|
|
99
|
+
<List height={600} itemCount={10000} itemSize={35} width={300}>
|
|
100
|
+
{({ index, style }) => <div style={style}>Row {index}</div>}
|
|
101
|
+
</List>
|
|
102
|
+
```
|
|
262
103
|
|
|
263
|
-
|
|
104
|
+
### 7. Image & Asset Optimization
|
|
264
105
|
|
|
265
|
-
|
|
106
|
+
```tsx
|
|
107
|
+
// ✅ Always use Next.js Image
|
|
108
|
+
import Image from 'next/image';
|
|
109
|
+
<Image src="/hero.jpg" alt="Hero" width={800} height={600} priority />
|
|
110
|
+
```
|
|
266
111
|
|
|
267
|
-
|
|
268
|
-
2. **Biggest impact first** - Waterfalls → Bundle → Server → Micro
|
|
269
|
-
3. **Don't over-optimize** - Focus on real bottlenecks
|
|
270
|
-
4. **Use platform features** - Next.js has optimizations built-in
|
|
271
|
-
5. **Think about users** - Real-world conditions matter
|
|
112
|
+
### 8. Advanced Patterns
|
|
272
113
|
|
|
273
|
-
|
|
114
|
+
```tsx
|
|
115
|
+
// ✅ useOptimistic for instant feedback
|
|
116
|
+
const [optimisticLikes, addOptimisticLike] = useOptimistic(likes);
|
|
274
117
|
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
118
|
+
// ✅ Server Actions for mutations
|
|
119
|
+
async function updateUser(formData: FormData) {
|
|
120
|
+
'use server';
|
|
121
|
+
await db.user.update({ ... });
|
|
122
|
+
revalidatePath('/profile');
|
|
123
|
+
}
|
|
124
|
+
```
|
|
280
125
|
|
|
281
|
-
|
|
126
|
+
## Decision Tree
|
|
282
127
|
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
128
|
+
```
|
|
129
|
+
Is data needed at render?
|
|
130
|
+
→ Yes → Use Server Component (RSC)
|
|
131
|
+
→ No, user-triggered → Use Server Action
|
|
132
|
+
→ Real-time/subscription → Client Component + SWR/WebSocket
|
|
133
|
+
```
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: react-patterns
|
|
3
|
+
description: "Modern React patterns and principles. Hooks, composition, performance, TypeScript best practices."
|
|
4
|
+
risk: unknown
|
|
5
|
+
source: community
|
|
6
|
+
date_added: "2026-02-27"
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
# React Patterns
|
|
10
|
+
|
|
11
|
+
> Principles for building production-ready React applications.
|
|
12
|
+
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
## 1. Component Design Principles
|
|
16
|
+
|
|
17
|
+
### Component Types
|
|
18
|
+
|
|
19
|
+
| Type | Use | State |
|
|
20
|
+
|------|-----|-------|
|
|
21
|
+
| **Server** | Data fetching, static | None |
|
|
22
|
+
| **Client** | Interactivity | useState, effects |
|
|
23
|
+
| **Presentational** | UI display | Props only |
|
|
24
|
+
| **Container** | Logic/state | Heavy state |
|
|
25
|
+
|
|
26
|
+
### Design Rules
|
|
27
|
+
|
|
28
|
+
- One responsibility per component
|
|
29
|
+
- Props down, events up
|
|
30
|
+
- Composition over inheritance
|
|
31
|
+
- Prefer small, focused components
|
|
32
|
+
|
|
33
|
+
---
|
|
34
|
+
|
|
35
|
+
## 2. Hook Patterns
|
|
36
|
+
|
|
37
|
+
### When to Extract Hooks
|
|
38
|
+
|
|
39
|
+
| Pattern | Extract When |
|
|
40
|
+
|---------|-------------|
|
|
41
|
+
| **useLocalStorage** | Same storage logic needed |
|
|
42
|
+
| **useDebounce** | Multiple debounced values |
|
|
43
|
+
| **useFetch** | Repeated fetch patterns |
|
|
44
|
+
| **useForm** | Complex form state |
|
|
45
|
+
|
|
46
|
+
### Hook Rules
|
|
47
|
+
|
|
48
|
+
- Hooks at top level only
|
|
49
|
+
- Same order every render
|
|
50
|
+
- Custom hooks start with "use"
|
|
51
|
+
- Clean up effects on unmount
|
|
52
|
+
|
|
53
|
+
---
|
|
54
|
+
|
|
55
|
+
## 3. State Management Selection
|
|
56
|
+
|
|
57
|
+
| Complexity | Solution |
|
|
58
|
+
|------------|----------|
|
|
59
|
+
| Simple | useState, useReducer |
|
|
60
|
+
| Shared local | Context |
|
|
61
|
+
| Server state | React Query, SWR |
|
|
62
|
+
| Complex global | Zustand, Redux Toolkit |
|
|
63
|
+
|
|
64
|
+
### State Placement
|
|
65
|
+
|
|
66
|
+
| Scope | Where |
|
|
67
|
+
|-------|-------|
|
|
68
|
+
| Single component | useState |
|
|
69
|
+
| Parent-child | Lift state up |
|
|
70
|
+
| Subtree | Context |
|
|
71
|
+
| App-wide | Global store |
|
|
72
|
+
|
|
73
|
+
---
|
|
74
|
+
|
|
75
|
+
## 4. React 19 Patterns
|
|
76
|
+
|
|
77
|
+
### New Hooks
|
|
78
|
+
|
|
79
|
+
| Hook | Purpose |
|
|
80
|
+
|------|---------|
|
|
81
|
+
| **useActionState** | Form submission state |
|
|
82
|
+
| **useOptimistic** | Optimistic UI updates |
|
|
83
|
+
| **use** | Read resources in render |
|
|
84
|
+
|
|
85
|
+
### Compiler Benefits
|
|
86
|
+
|
|
87
|
+
- Automatic memoization
|
|
88
|
+
- Less manual useMemo/useCallback
|
|
89
|
+
- Focus on pure components
|
|
90
|
+
|
|
91
|
+
---
|
|
92
|
+
|
|
93
|
+
## 5. Composition Patterns
|
|
94
|
+
|
|
95
|
+
### Compound Components
|
|
96
|
+
|
|
97
|
+
- Parent provides context
|
|
98
|
+
- Children consume context
|
|
99
|
+
- Flexible slot-based composition
|
|
100
|
+
- Example: Tabs, Accordion, Dropdown
|
|
101
|
+
|
|
102
|
+
### Render Props vs Hooks
|
|
103
|
+
|
|
104
|
+
| Use Case | Prefer |
|
|
105
|
+
|----------|--------|
|
|
106
|
+
| Reusable logic | Custom hook |
|
|
107
|
+
| Render flexibility | Render props |
|
|
108
|
+
| Cross-cutting | Higher-order component |
|
|
109
|
+
|
|
110
|
+
---
|
|
111
|
+
|
|
112
|
+
## 6. Performance Principles
|
|
113
|
+
|
|
114
|
+
### When to Optimize
|
|
115
|
+
|
|
116
|
+
| Signal | Action |
|
|
117
|
+
|--------|--------|
|
|
118
|
+
| Slow renders | Profile first |
|
|
119
|
+
| Large lists | Virtualize |
|
|
120
|
+
| Expensive calc | useMemo |
|
|
121
|
+
| Stable callbacks | useCallback |
|
|
122
|
+
|
|
123
|
+
### Optimization Order
|
|
124
|
+
|
|
125
|
+
1. Check if actually slow
|
|
126
|
+
2. Profile with DevTools
|
|
127
|
+
3. Identify bottleneck
|
|
128
|
+
4. Apply targeted fix
|
|
129
|
+
|
|
130
|
+
---
|
|
131
|
+
|
|
132
|
+
## 7. Error Handling
|
|
133
|
+
|
|
134
|
+
### Error Boundary Usage
|
|
135
|
+
|
|
136
|
+
| Scope | Placement |
|
|
137
|
+
|-------|-----------|
|
|
138
|
+
| App-wide | Root level |
|
|
139
|
+
| Feature | Route/feature level |
|
|
140
|
+
| Component | Around risky component |
|
|
141
|
+
|
|
142
|
+
### Error Recovery
|
|
143
|
+
|
|
144
|
+
- Show fallback UI
|
|
145
|
+
- Log error
|
|
146
|
+
- Offer retry option
|
|
147
|
+
- Preserve user data
|
|
148
|
+
|
|
149
|
+
---
|
|
150
|
+
|
|
151
|
+
## 8. TypeScript Patterns
|
|
152
|
+
|
|
153
|
+
### Props Typing
|
|
154
|
+
|
|
155
|
+
| Pattern | Use |
|
|
156
|
+
|---------|-----|
|
|
157
|
+
| Interface | Component props |
|
|
158
|
+
| Type | Unions, complex |
|
|
159
|
+
| Generic | Reusable components |
|
|
160
|
+
|
|
161
|
+
### Common Types
|
|
162
|
+
|
|
163
|
+
| Need | Type |
|
|
164
|
+
|------|------|
|
|
165
|
+
| Children | ReactNode |
|
|
166
|
+
| Event handler | MouseEventHandler |
|
|
167
|
+
| Ref | RefObject<Element> |
|
|
168
|
+
|
|
169
|
+
---
|
|
170
|
+
|
|
171
|
+
## 9. Testing Principles
|
|
172
|
+
|
|
173
|
+
| Level | Focus |
|
|
174
|
+
|-------|-------|
|
|
175
|
+
| Unit | Pure functions, hooks |
|
|
176
|
+
| Integration | Component behavior |
|
|
177
|
+
| E2E | User flows |
|
|
178
|
+
|
|
179
|
+
### Test Priorities
|
|
180
|
+
|
|
181
|
+
- User-visible behavior
|
|
182
|
+
- Edge cases
|
|
183
|
+
- Error states
|
|
184
|
+
- Accessibility
|
|
185
|
+
|
|
186
|
+
---
|
|
187
|
+
|
|
188
|
+
## 10. Anti-Patterns
|
|
189
|
+
|
|
190
|
+
| ❌ Don't | ✅ Do |
|
|
191
|
+
|----------|-------|
|
|
192
|
+
| Prop drilling deep | Use context |
|
|
193
|
+
| Giant components | Split smaller |
|
|
194
|
+
| useEffect for everything | Server components |
|
|
195
|
+
| Premature optimization | Profile first |
|
|
196
|
+
| Index as key | Stable unique ID |
|
|
197
|
+
|
|
198
|
+
---
|
|
199
|
+
|
|
200
|
+
> **Remember:** React is about composition. Build small, combine thoughtfully.
|