@ranimontagna/agent-toolkit 0.1.5 → 0.1.6
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/README.md +42 -8
- package/package.json +1 -1
- package/skills/frontend/react/react-patterns/LICENSE +21 -0
- package/skills/frontend/react/react-patterns/NOTICE.md +11 -0
- package/skills/frontend/react/react-patterns/SKILL.md +341 -0
- package/skills/frontend/react/react-performance/LICENSE +21 -0
- package/skills/frontend/react/react-performance/NOTICE.md +11 -0
- package/skills/frontend/react/react-performance/SKILL.md +574 -0
- package/skills/frontend/react/react-testing/LICENSE +21 -0
- package/skills/frontend/react/react-testing/NOTICE.md +11 -0
- package/skills/frontend/react/react-testing/SKILL.md +423 -0
- package/skills/frontend/react-native/react-native-expert/LICENSE +21 -0
- package/skills/frontend/react-native/react-native-expert/NOTICE.md +11 -0
- package/skills/frontend/react-native/react-native-expert/SKILL.md +187 -0
- package/skills/frontend/react-native/react-native-expert/references/expo-router.md +187 -0
- package/skills/frontend/react-native/react-native-expert/references/list-optimization.md +204 -0
- package/skills/frontend/react-native/react-native-expert/references/platform-handling.md +188 -0
- package/skills/frontend/react-native/react-native-expert/references/project-structure.md +171 -0
- package/skills/frontend/react-native/react-native-expert/references/storage-hooks.md +173 -0
- package/skills/frontend/react-native/react-native-unistyles-v3/LICENSE +21 -0
- package/skills/frontend/react-native/react-native-unistyles-v3/NOTICE.md +11 -0
- package/skills/frontend/react-native/react-native-unistyles-v3/SKILL.md +159 -0
- package/skills/frontend/react-native/react-native-unistyles-v3/references/api-reference.md +495 -0
- package/skills/frontend/react-native/react-native-unistyles-v3/references/common-issues.md +389 -0
- package/skills/frontend/react-native/react-native-unistyles-v3/references/setup-guide.md +217 -0
- package/skills/frontend/react-native/react-native-unistyles-v3/references/styling-patterns.md +705 -0
- package/skills/frontend/react-native/react-native-unistyles-v3/references/third-party-integration.md +318 -0
|
@@ -0,0 +1,574 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: react-performance
|
|
3
|
+
description: React and Next.js performance optimization patterns adapted from Vercel Engineering's React Best Practices (https://github.com/vercel-labs/agent-skills). Organizes 70+ rules across 8 priority categories — waterfalls, bundle size, server-side, client fetching, re-render, rendering, JS micro-perf, advanced. Use when writing, reviewing, or refactoring React/Next.js code for performance.
|
|
4
|
+
origin: ECC
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
# React Performance
|
|
8
|
+
|
|
9
|
+
Performance optimization patterns for React 18/19 and Next.js, adapted from [Vercel Labs `react-best-practices`](https://github.com/vercel-labs/agent-skills/tree/main/skills/react-best-practices) (MIT, v1.0.0). This skill organizes rules by priority and provides decision-tree guidance for active code review and refactoring.
|
|
10
|
+
|
|
11
|
+
## When to Activate
|
|
12
|
+
|
|
13
|
+
- Writing or reviewing React/Next.js code for performance
|
|
14
|
+
- Diagnosing slow page loads, slow interactions, or high CPU on the client
|
|
15
|
+
- Auditing bundle size or Lighthouse Core Web Vitals regressions
|
|
16
|
+
- Removing waterfalls in Server Components / API routes
|
|
17
|
+
- Reducing client-side re-renders
|
|
18
|
+
- Optimizing long lists, animations, or hydration
|
|
19
|
+
- Auditing optimization choices in PRs touching `app/`, `pages/`, `components/`, or data layers
|
|
20
|
+
|
|
21
|
+
## Priority Index
|
|
22
|
+
|
|
23
|
+
| Priority | Category | Prefix | When it matters |
|
|
24
|
+
|---|---|---|---|
|
|
25
|
+
| 1 — CRITICAL | Eliminating Waterfalls | `async-` | Anytime `await` is followed by independent `await` |
|
|
26
|
+
| 2 — CRITICAL | Bundle Size Optimization | `bundle-` | First-load JS, route-level imports, third-party libs |
|
|
27
|
+
| 3 — HIGH | Server-Side Performance | `server-` | RSC, Server Actions, API routes, SSR |
|
|
28
|
+
| 4 — MEDIUM-HIGH | Client-Side Data Fetching | `client-` | SWR / TanStack Query / raw `fetch` in hooks |
|
|
29
|
+
| 5 — MEDIUM | Re-render Optimization | `rerender-` | High-frequency state updates, parent-child fan-out |
|
|
30
|
+
| 6 — MEDIUM | Rendering Performance | `rendering-` | Long lists, animations, hydration |
|
|
31
|
+
| 7 — LOW-MEDIUM | JavaScript Performance | `js-` | Hot loops, frequent allocations |
|
|
32
|
+
| 8 — LOW | Advanced Patterns | `advanced-` | Effect-event integration, stable refs |
|
|
33
|
+
|
|
34
|
+
## 1. Eliminating Waterfalls (CRITICAL)
|
|
35
|
+
|
|
36
|
+
> "Waterfalls are the #1 performance killer" — every sequential `await` adds full network latency.
|
|
37
|
+
|
|
38
|
+
### Cheap conditions before await
|
|
39
|
+
|
|
40
|
+
Check sync conditions (props, env, hardcoded flags) before awaiting remote data.
|
|
41
|
+
|
|
42
|
+
```ts
|
|
43
|
+
// INCORRECT
|
|
44
|
+
async function Page({ id }: { id: string }) {
|
|
45
|
+
const flag = await getFlag("show-page");
|
|
46
|
+
if (!flag || !id) return null;
|
|
47
|
+
const data = await getData(id);
|
|
48
|
+
// ...
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// CORRECT — short-circuit on cheap sync condition first
|
|
52
|
+
async function Page({ id }: { id: string }) {
|
|
53
|
+
if (!id) return null;
|
|
54
|
+
const flag = await getFlag("show-page");
|
|
55
|
+
if (!flag) return null;
|
|
56
|
+
const data = await getData(id);
|
|
57
|
+
}
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### Defer awaits until used
|
|
61
|
+
|
|
62
|
+
Move `await` into the branch that uses it.
|
|
63
|
+
|
|
64
|
+
```ts
|
|
65
|
+
// INCORRECT — awaits before deciding it needs the data
|
|
66
|
+
const user = await getUser(id);
|
|
67
|
+
if (mode === "guest") return renderGuest();
|
|
68
|
+
return renderUser(user);
|
|
69
|
+
|
|
70
|
+
// CORRECT
|
|
71
|
+
if (mode === "guest") return renderGuest();
|
|
72
|
+
const user = await getUser(id);
|
|
73
|
+
return renderUser(user);
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### Promise.all for independent work
|
|
77
|
+
|
|
78
|
+
```ts
|
|
79
|
+
// INCORRECT — sequential
|
|
80
|
+
const user = await getUser(id);
|
|
81
|
+
const posts = await getPosts(id);
|
|
82
|
+
const followers = await getFollowers(id);
|
|
83
|
+
|
|
84
|
+
// CORRECT — parallel
|
|
85
|
+
const [user, posts, followers] = await Promise.all([
|
|
86
|
+
getUser(id),
|
|
87
|
+
getPosts(id),
|
|
88
|
+
getFollowers(id),
|
|
89
|
+
]);
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### Partial dependencies — start early, await late
|
|
93
|
+
|
|
94
|
+
```ts
|
|
95
|
+
// CORRECT — kick off all promises, await only when each result is needed
|
|
96
|
+
const userP = getUser(id);
|
|
97
|
+
const postsP = getPosts(id);
|
|
98
|
+
const profile = await getProfile(id);
|
|
99
|
+
if (profile.private) return null;
|
|
100
|
+
const [user, posts] = await Promise.all([userP, postsP]);
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
### Suspense for streaming
|
|
104
|
+
|
|
105
|
+
Push `<Suspense>` boundaries close to the data so the page paints what it can while slower sub-trees stream in. The trade-off: layout shift when content arrives — reserve space (skeleton or `min-height`).
|
|
106
|
+
|
|
107
|
+
### Server Components: parallel through composition
|
|
108
|
+
|
|
109
|
+
```tsx
|
|
110
|
+
// INCORRECT — sibling awaits run sequentially inside one component
|
|
111
|
+
export default async function Page() {
|
|
112
|
+
const user = await getUser();
|
|
113
|
+
const cart = await getCart();
|
|
114
|
+
return <View user={user} cart={cart} />;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// CORRECT — split into children, React runs them in parallel
|
|
118
|
+
export default async function Page() {
|
|
119
|
+
return (
|
|
120
|
+
<View>
|
|
121
|
+
<UserSection />
|
|
122
|
+
<CartSection />
|
|
123
|
+
</View>
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
## 2. Bundle Size Optimization (CRITICAL)
|
|
129
|
+
|
|
130
|
+
### Direct imports, not barrels
|
|
131
|
+
|
|
132
|
+
Barrel `index.ts` files force the bundler to walk the entire module graph even when tree-shaking removes most of it. Direct imports save 200-800ms of first-load JS in many real-world apps.
|
|
133
|
+
|
|
134
|
+
```ts
|
|
135
|
+
// INCORRECT
|
|
136
|
+
import { Button, Card, Modal } from "@/components";
|
|
137
|
+
|
|
138
|
+
// CORRECT
|
|
139
|
+
import { Button } from "@/components/Button";
|
|
140
|
+
import { Card } from "@/components/Card";
|
|
141
|
+
import { Modal } from "@/components/Modal";
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
Next.js 13.5+ has [Optimize Package Imports](https://nextjs.org/docs/app/api-reference/next-config-js/optimizePackageImports) that automates this for listed packages — use it; manual direct imports still required for non-listed libs.
|
|
145
|
+
|
|
146
|
+
### Statically analyzable paths
|
|
147
|
+
|
|
148
|
+
```ts
|
|
149
|
+
// INCORRECT — defeats bundler/trace analysis
|
|
150
|
+
const mod = await import(`./pages/${name}`);
|
|
151
|
+
|
|
152
|
+
// CORRECT — explicit per branch
|
|
153
|
+
const mod = name === "home" ? await import("./pages/home") : await import("./pages/about");
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
### Dynamic imports for heavy components
|
|
157
|
+
|
|
158
|
+
```tsx
|
|
159
|
+
import dynamic from "next/dynamic";
|
|
160
|
+
|
|
161
|
+
const HeavyChart = dynamic(() => import("./HeavyChart"), {
|
|
162
|
+
loading: () => <Skeleton />,
|
|
163
|
+
ssr: false, // when client-only
|
|
164
|
+
});
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
### Defer third-party scripts
|
|
168
|
+
|
|
169
|
+
Load analytics, logging, support widgets AFTER hydration. Use `next/script` with `strategy="afterInteractive"` (default) or `"lazyOnload"`.
|
|
170
|
+
|
|
171
|
+
### Conditional module loading
|
|
172
|
+
|
|
173
|
+
```tsx
|
|
174
|
+
if (user.role === "admin") {
|
|
175
|
+
const { AdminPanel } = await import("./admin/AdminPanel");
|
|
176
|
+
// ...
|
|
177
|
+
}
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
### Preload on hover/focus
|
|
181
|
+
|
|
182
|
+
Trigger `<link rel="preload">` or `import()` on hover so the bundle is in cache by the time the user clicks.
|
|
183
|
+
|
|
184
|
+
## 3. Server-Side Performance (HIGH)
|
|
185
|
+
|
|
186
|
+
### Authenticate Server Actions like API routes
|
|
187
|
+
|
|
188
|
+
Every `"use server"` function is a public endpoint. Authenticate AND authorize inside the action — never rely on the calling Client Component's gating.
|
|
189
|
+
|
|
190
|
+
```ts
|
|
191
|
+
"use server";
|
|
192
|
+
export async function deleteUser(formData: FormData) {
|
|
193
|
+
const session = await getSession();
|
|
194
|
+
if (!session?.user) throw new Error("Unauthorized");
|
|
195
|
+
const targetId = String(formData.get("id"));
|
|
196
|
+
if (session.user.role !== "admin" && session.user.id !== targetId) {
|
|
197
|
+
throw new Error("Forbidden");
|
|
198
|
+
}
|
|
199
|
+
await db.user.delete({ where: { id: targetId } });
|
|
200
|
+
}
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
### `React.cache()` for per-request deduplication
|
|
204
|
+
|
|
205
|
+
```ts
|
|
206
|
+
import { cache } from "react";
|
|
207
|
+
|
|
208
|
+
export const getUser = cache(async (id: string) => {
|
|
209
|
+
return db.user.findUnique({ where: { id } });
|
|
210
|
+
});
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
`React.cache` dedupes within a single request. Calling `getUser("1")` from three Server Components in the same render = one DB query.
|
|
214
|
+
|
|
215
|
+
### LRU cache for cross-request data
|
|
216
|
+
|
|
217
|
+
For data that does NOT change per request (config, lookup tables), cache outside React with an LRU cache or `unstable_cache`.
|
|
218
|
+
|
|
219
|
+
### Avoid duplicate serialization in RSC props
|
|
220
|
+
|
|
221
|
+
When a Server Component renders the same data into multiple Client Components, the data is serialized once per consumer. Lift the Client Component up and pass children.
|
|
222
|
+
|
|
223
|
+
### Hoist static I/O to module scope
|
|
224
|
+
|
|
225
|
+
```ts
|
|
226
|
+
// CORRECT — runs once at module load
|
|
227
|
+
const fontData = readFileSync(fontPath);
|
|
228
|
+
|
|
229
|
+
export async function Page() {
|
|
230
|
+
return <Banner font={fontData} />;
|
|
231
|
+
}
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
### No mutable module-level state in RSC/SSR
|
|
235
|
+
|
|
236
|
+
Module state on the server is shared across all requests — a race condition between users. Use request-scoped storage (`headers()`, `cookies()`, async context) instead.
|
|
237
|
+
|
|
238
|
+
### Minimize data passed to Client Components
|
|
239
|
+
|
|
240
|
+
Only serialize what the Client needs. Strip fields, paginate, project columns at the DB layer.
|
|
241
|
+
|
|
242
|
+
### Parallelize nested fetches with Promise.all per item
|
|
243
|
+
|
|
244
|
+
```ts
|
|
245
|
+
const users = await getUsers();
|
|
246
|
+
const enriched = await Promise.all(
|
|
247
|
+
users.map(async (u) => ({ ...u, posts: await getPostsFor(u.id) })),
|
|
248
|
+
);
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
### Use `after()` for non-blocking work
|
|
252
|
+
|
|
253
|
+
Next.js 15 `after()` runs work after the response is sent — logging, cache warming, analytics.
|
|
254
|
+
|
|
255
|
+
```ts
|
|
256
|
+
import { after } from "next/server";
|
|
257
|
+
export async function GET() {
|
|
258
|
+
const data = await getData();
|
|
259
|
+
after(() => logAnalytics(data));
|
|
260
|
+
return Response.json(data);
|
|
261
|
+
}
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
## 4. Client-Side Data Fetching (MEDIUM-HIGH)
|
|
265
|
+
|
|
266
|
+
### SWR / TanStack Query for deduplication
|
|
267
|
+
|
|
268
|
+
Multiple components calling `useUser(id)` should share one network request and one cache entry. Use SWR or TanStack Query — never roll your own `useEffect` + `fetch` for shared data.
|
|
269
|
+
|
|
270
|
+
### Deduplicate global event listeners
|
|
271
|
+
|
|
272
|
+
```tsx
|
|
273
|
+
// INCORRECT — every component adds its own
|
|
274
|
+
useEffect(() => {
|
|
275
|
+
window.addEventListener("scroll", handler);
|
|
276
|
+
return () => window.removeEventListener("scroll", handler);
|
|
277
|
+
}, []);
|
|
278
|
+
|
|
279
|
+
// CORRECT — single shared listener via a hook + global subject
|
|
280
|
+
const useScroll = createScrollHook(); // singleton subject under the hood
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
### Passive listeners for scroll
|
|
284
|
+
|
|
285
|
+
```ts
|
|
286
|
+
window.addEventListener("scroll", handler, { passive: true });
|
|
287
|
+
```
|
|
288
|
+
|
|
289
|
+
Improves scrolling smoothness; the listener cannot `preventDefault()`.
|
|
290
|
+
|
|
291
|
+
### localStorage: version + minimize
|
|
292
|
+
|
|
293
|
+
- Always store a `version` field; bump on schema change and migrate or discard old data
|
|
294
|
+
- Keep payloads small — `localStorage` is synchronous and blocks main thread
|
|
295
|
+
|
|
296
|
+
## 5. Re-render Optimization (MEDIUM)
|
|
297
|
+
|
|
298
|
+
### Don't subscribe to state used only in callbacks
|
|
299
|
+
|
|
300
|
+
```tsx
|
|
301
|
+
// INCORRECT — re-renders every time count changes
|
|
302
|
+
const count = useStore((s) => s.count);
|
|
303
|
+
const handler = () => doSomething(count);
|
|
304
|
+
|
|
305
|
+
// CORRECT — read once on call
|
|
306
|
+
const handler = () => {
|
|
307
|
+
const count = useStore.getState().count;
|
|
308
|
+
doSomething(count);
|
|
309
|
+
};
|
|
310
|
+
```
|
|
311
|
+
|
|
312
|
+
### Extract expensive work into memoized components
|
|
313
|
+
|
|
314
|
+
```tsx
|
|
315
|
+
// CORRECT — child re-renders only when `items` changes
|
|
316
|
+
const Heavy = memo(function Heavy({ items }: { items: Item[] }) {
|
|
317
|
+
return <Chart data={transform(items)} />;
|
|
318
|
+
});
|
|
319
|
+
```
|
|
320
|
+
|
|
321
|
+
### Hoist default non-primitive props
|
|
322
|
+
|
|
323
|
+
```tsx
|
|
324
|
+
// INCORRECT — new array each render breaks memo
|
|
325
|
+
<List items={items ?? []} />
|
|
326
|
+
|
|
327
|
+
// CORRECT
|
|
328
|
+
const EMPTY: Item[] = [];
|
|
329
|
+
<List items={items ?? EMPTY} />
|
|
330
|
+
```
|
|
331
|
+
|
|
332
|
+
### Primitive dependencies in effects
|
|
333
|
+
|
|
334
|
+
```tsx
|
|
335
|
+
// INCORRECT — new object identity every render
|
|
336
|
+
useEffect(() => {}, [{ id, name }]);
|
|
337
|
+
|
|
338
|
+
// CORRECT — primitives
|
|
339
|
+
useEffect(() => {}, [id, name]);
|
|
340
|
+
```
|
|
341
|
+
|
|
342
|
+
### Subscribe to derived booleans, not raw values
|
|
343
|
+
|
|
344
|
+
```tsx
|
|
345
|
+
// INCORRECT — re-renders for any cart change
|
|
346
|
+
const cart = useStore((s) => s.cart);
|
|
347
|
+
const hasItems = cart.length > 0;
|
|
348
|
+
|
|
349
|
+
// CORRECT — re-renders only when emptiness flips
|
|
350
|
+
const hasItems = useStore((s) => s.cart.length > 0);
|
|
351
|
+
```
|
|
352
|
+
|
|
353
|
+
### Derive during render, never via `useEffect`
|
|
354
|
+
|
|
355
|
+
```tsx
|
|
356
|
+
// INCORRECT
|
|
357
|
+
const [full, setFull] = useState("");
|
|
358
|
+
useEffect(() => setFull(`${first} ${last}`), [first, last]);
|
|
359
|
+
|
|
360
|
+
// CORRECT
|
|
361
|
+
const full = `${first} ${last}`;
|
|
362
|
+
```
|
|
363
|
+
|
|
364
|
+
### Functional `setState` for stable callbacks
|
|
365
|
+
|
|
366
|
+
```tsx
|
|
367
|
+
// CORRECT
|
|
368
|
+
const increment = useCallback(() => setCount((c) => c + 1), []);
|
|
369
|
+
```
|
|
370
|
+
|
|
371
|
+
### Lazy state initializer for expensive values
|
|
372
|
+
|
|
373
|
+
```tsx
|
|
374
|
+
const [tree] = useState(() => parseTree(largeInput));
|
|
375
|
+
```
|
|
376
|
+
|
|
377
|
+
### Avoid memo for simple primitives
|
|
378
|
+
|
|
379
|
+
`useMemo(() => x + 1, [x])` is overhead. Memo earns its keep on object identity and expensive computation.
|
|
380
|
+
|
|
381
|
+
### Split hooks with independent deps
|
|
382
|
+
|
|
383
|
+
```tsx
|
|
384
|
+
// INCORRECT — both selectors re-run if either source changes
|
|
385
|
+
const { a, b } = useSomething(source1, source2);
|
|
386
|
+
|
|
387
|
+
// CORRECT
|
|
388
|
+
const a = useA(source1);
|
|
389
|
+
const b = useB(source2);
|
|
390
|
+
```
|
|
391
|
+
|
|
392
|
+
### Move interaction logic into event handlers
|
|
393
|
+
|
|
394
|
+
Event handlers run only on the user action — `useEffect` re-runs whenever deps change.
|
|
395
|
+
|
|
396
|
+
### `startTransition` for non-urgent updates
|
|
397
|
+
|
|
398
|
+
```tsx
|
|
399
|
+
const [pending, startTransition] = useTransition();
|
|
400
|
+
startTransition(() => setFilters(newFilters));
|
|
401
|
+
```
|
|
402
|
+
|
|
403
|
+
### `useDeferredValue` for expensive renders
|
|
404
|
+
|
|
405
|
+
```tsx
|
|
406
|
+
const deferredQuery = useDeferredValue(query);
|
|
407
|
+
const results = useMemo(() => expensiveSearch(deferredQuery), [deferredQuery]);
|
|
408
|
+
```
|
|
409
|
+
|
|
410
|
+
### `useRef` for transient frequent values
|
|
411
|
+
|
|
412
|
+
For values that change often but should not trigger re-render (timestamps, last-key, accumulators).
|
|
413
|
+
|
|
414
|
+
### Don't define components inside components
|
|
415
|
+
|
|
416
|
+
```tsx
|
|
417
|
+
// INCORRECT — Inner is a new component on every Outer render
|
|
418
|
+
function Outer() {
|
|
419
|
+
const Inner = () => <span />;
|
|
420
|
+
return <Inner />;
|
|
421
|
+
}
|
|
422
|
+
```
|
|
423
|
+
|
|
424
|
+
Each render makes a new `Inner` type, defeating reconciliation and unmounting children.
|
|
425
|
+
|
|
426
|
+
## 6. Rendering Performance (MEDIUM)
|
|
427
|
+
|
|
428
|
+
### Animate the wrapper, not the SVG
|
|
429
|
+
|
|
430
|
+
Transforming a `<div>` wrapper around an SVG is GPU-accelerated; transforming the SVG itself triggers paint.
|
|
431
|
+
|
|
432
|
+
### `content-visibility: auto` for long lists
|
|
433
|
+
|
|
434
|
+
```css
|
|
435
|
+
.row { content-visibility: auto; contain-intrinsic-size: auto 80px; }
|
|
436
|
+
```
|
|
437
|
+
|
|
438
|
+
Browser skips offscreen rendering — major win for lists with hundreds of rows.
|
|
439
|
+
|
|
440
|
+
### Hoist static JSX
|
|
441
|
+
|
|
442
|
+
```tsx
|
|
443
|
+
const STATIC_HEADER = <h1>Title</h1>;
|
|
444
|
+
function Page() {
|
|
445
|
+
return <>{STATIC_HEADER}<Body /></>;
|
|
446
|
+
}
|
|
447
|
+
```
|
|
448
|
+
|
|
449
|
+
### SVG: reduce coordinate precision
|
|
450
|
+
|
|
451
|
+
`d="M10.123456,20.654321"` → `d="M10.12,20.65"`. Each digit costs bytes; the visual difference is sub-pixel.
|
|
452
|
+
|
|
453
|
+
### Hydration no-flicker via inline script
|
|
454
|
+
|
|
455
|
+
For values needed before hydration (theme, locale), inline a `<script>` that sets `document.documentElement.dataset.*` before React mounts.
|
|
456
|
+
|
|
457
|
+
### Suppress expected hydration mismatches narrowly
|
|
458
|
+
|
|
459
|
+
```tsx
|
|
460
|
+
<time suppressHydrationWarning>{new Date().toLocaleString()}</time>
|
|
461
|
+
```
|
|
462
|
+
|
|
463
|
+
Use ONLY for known-divergent leaf nodes — never on a tree containing other children.
|
|
464
|
+
|
|
465
|
+
### `<Activity>` for show/hide instead of mount/unmount
|
|
466
|
+
|
|
467
|
+
React 19 `<Activity mode="visible|hidden">` keeps tree state and effects mounted but hides — cheaper than unmount/remount for tabs and accordions.
|
|
468
|
+
|
|
469
|
+
### Ternary over `&&` for conditional render
|
|
470
|
+
|
|
471
|
+
```tsx
|
|
472
|
+
// INCORRECT — `0` renders as text node
|
|
473
|
+
{count && <Badge>{count}</Badge>}
|
|
474
|
+
|
|
475
|
+
// CORRECT
|
|
476
|
+
{count > 0 ? <Badge>{count}</Badge> : null}
|
|
477
|
+
```
|
|
478
|
+
|
|
479
|
+
### `useTransition` for loading states
|
|
480
|
+
|
|
481
|
+
Pair `startTransition` with the action; React shows the previous UI as `isPending` while the next state computes.
|
|
482
|
+
|
|
483
|
+
### React DOM resource hints
|
|
484
|
+
|
|
485
|
+
```tsx
|
|
486
|
+
import { preload, preconnect } from "react-dom";
|
|
487
|
+
preload("/api/critical", { as: "fetch" });
|
|
488
|
+
preconnect("https://api.example.com");
|
|
489
|
+
```
|
|
490
|
+
|
|
491
|
+
### `defer` / `async` on `<script>` tags
|
|
492
|
+
|
|
493
|
+
`defer` for ordered execution after DOMContentLoaded; `async` for fire-and-forget.
|
|
494
|
+
|
|
495
|
+
## 7. JavaScript Performance (LOW-MEDIUM)
|
|
496
|
+
|
|
497
|
+
- **Batch DOM/CSS changes** — apply via class swap or `cssText`, not property-by-property
|
|
498
|
+
- **`Map` for repeated lookups** — `O(1)` vs `O(n)` linear scan
|
|
499
|
+
- **Cache property access in loops** — `const len = arr.length`
|
|
500
|
+
- **Memoize pure functions** — module-level `Map<key, result>`
|
|
501
|
+
- **Cache `localStorage` reads** — sync API; one read per render
|
|
502
|
+
- **Combine `filter().map()` into one pass** — `flatMap` or single `for`
|
|
503
|
+
- **Check array length first** before expensive comparisons
|
|
504
|
+
- **Early return** from functions
|
|
505
|
+
- **Hoist RegExp** out of loops — compilation is not free
|
|
506
|
+
- **Loop for min/max** instead of `sort()` — `O(n)` vs `O(n log n)`
|
|
507
|
+
- **`Set`/`Map` for membership** — `O(1)` vs `Array.includes` `O(n)`
|
|
508
|
+
- **`toSorted()` over mutation** when immutability matters
|
|
509
|
+
- **`flatMap` to map and filter in one pass**
|
|
510
|
+
- **`requestIdleCallback`** for non-critical work
|
|
511
|
+
|
|
512
|
+
## 8. Advanced Patterns (LOW)
|
|
513
|
+
|
|
514
|
+
### `useEffectEvent` deps
|
|
515
|
+
|
|
516
|
+
Values from `useEffectEvent` are stable — do NOT add them to effect deps.
|
|
517
|
+
|
|
518
|
+
### Event handler refs
|
|
519
|
+
|
|
520
|
+
For stable callbacks passed to memoized children:
|
|
521
|
+
|
|
522
|
+
```tsx
|
|
523
|
+
const handlerRef = useRef(handler);
|
|
524
|
+
useEffect(() => { handlerRef.current = handler; });
|
|
525
|
+
const stable = useCallback((arg) => handlerRef.current(arg), []);
|
|
526
|
+
```
|
|
527
|
+
|
|
528
|
+
### Init once per app load
|
|
529
|
+
|
|
530
|
+
For module-level singletons (telemetry, logger), guard with a module-scope flag — not `useEffect`.
|
|
531
|
+
|
|
532
|
+
### `useLatest` for stable callback refs
|
|
533
|
+
|
|
534
|
+
```tsx
|
|
535
|
+
function useLatest<T>(value: T) {
|
|
536
|
+
const ref = useRef(value);
|
|
537
|
+
ref.current = value;
|
|
538
|
+
return ref;
|
|
539
|
+
}
|
|
540
|
+
```
|
|
541
|
+
|
|
542
|
+
## Automated Tools
|
|
543
|
+
|
|
544
|
+
Many of these rules are now automated:
|
|
545
|
+
|
|
546
|
+
- **Next.js 13.5+ Optimize Package Imports** — barrel import optimization
|
|
547
|
+
- **React Compiler** (RFC, in canary) — auto-memoization
|
|
548
|
+
- **Turbopack** — faster builds, better tree-shaking
|
|
549
|
+
- **Bundle Analyzer** (`@next/bundle-analyzer`) — visualize first-load JS
|
|
550
|
+
|
|
551
|
+
When the project ships React Compiler, demote `rerender-*` manual memoization rules to "review-only" — the compiler handles them. Manual `useMemo`/`useCallback` becomes unnecessary noise.
|
|
552
|
+
|
|
553
|
+
## Lighthouse / Web Vitals Mapping
|
|
554
|
+
|
|
555
|
+
| Metric | Most relevant categories |
|
|
556
|
+
|---|---|
|
|
557
|
+
| **LCP** (Largest Contentful Paint) | Waterfalls, Bundle Size, Resource Hints |
|
|
558
|
+
| **INP** (Interaction to Next Paint) | Re-render, Rendering, JavaScript |
|
|
559
|
+
| **CLS** (Cumulative Layout Shift) | Rendering (Suspense placement, image dimensions) |
|
|
560
|
+
| **TBT** (Total Blocking Time) | Bundle Size, JavaScript, Defer Third-Party |
|
|
561
|
+
| **FID** (legacy) | Bundle Size, Hydration |
|
|
562
|
+
|
|
563
|
+
## Related
|
|
564
|
+
|
|
565
|
+
- Skills: [react-patterns](../react-patterns/SKILL.md), [react-testing](../react-testing/SKILL.md), [frontend-patterns](../frontend-patterns/SKILL.md), [accessibility](../accessibility/SKILL.md), [nextjs-turbopack](../nextjs-turbopack/SKILL.md)
|
|
566
|
+
- Rules: [rules/react/](../../rules/react/)
|
|
567
|
+
- Agents: `react-reviewer` enforces these rules in code review; `react-build-resolver` handles related build failures
|
|
568
|
+
- Commands: `/react-review`, `/react-build`, `/react-test`
|
|
569
|
+
|
|
570
|
+
## Attribution
|
|
571
|
+
|
|
572
|
+
Adapted from Vercel Labs `react-best-practices` skill (MIT License, copyright Vercel Engineering, v1.0.0 January 2026). Source: [https://github.com/vercel-labs/agent-skills/tree/main/skills/react-best-practices](https://github.com/vercel-labs/agent-skills/tree/main/skills/react-best-practices).
|
|
573
|
+
|
|
574
|
+
This skill restructures and adapts the original 70-rule catalog into a single navigable reference. For the full original ruleset with extended examples, see the upstream repository.
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Affaan Mustafa
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# Attribution
|
|
2
|
+
|
|
3
|
+
This skill is copied from Affaan Mustafa's public `affaan-m/ECC` repository.
|
|
4
|
+
|
|
5
|
+
- Source: https://github.com/affaan-m/ECC/tree/main/skills/react-testing
|
|
6
|
+
- Imported from commit: `0f84c0e2796703fbda87d577b2636351418c7442`
|
|
7
|
+
- Upstream skill name: `react-testing`
|
|
8
|
+
- License: MIT
|
|
9
|
+
- Copyright: Copyright (c) 2026 Affaan Mustafa
|
|
10
|
+
|
|
11
|
+
The upstream MIT license text is preserved in `LICENSE`.
|