@kood/claude-code 0.3.10 → 0.3.12
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/dist/index.js +1 -1
- package/package.json +1 -1
- package/templates/.claude/skills/vercel-react-best-practices/AGENTS.md +2249 -0
- package/templates/.claude/skills/vercel-react-best-practices/SKILL.md +125 -0
- package/templates/.claude/skills/vercel-react-best-practices/rules/advanced-event-handler-refs.md +55 -0
- package/templates/.claude/skills/vercel-react-best-practices/rules/advanced-use-latest.md +49 -0
- package/templates/.claude/skills/vercel-react-best-practices/rules/async-api-routes.md +38 -0
- package/templates/.claude/skills/vercel-react-best-practices/rules/async-defer-await.md +80 -0
- package/templates/.claude/skills/vercel-react-best-practices/rules/async-dependencies.md +36 -0
- package/templates/.claude/skills/vercel-react-best-practices/rules/async-parallel.md +28 -0
- package/templates/.claude/skills/vercel-react-best-practices/rules/async-suspense-boundaries.md +99 -0
- package/templates/.claude/skills/vercel-react-best-practices/rules/bundle-barrel-imports.md +59 -0
- package/templates/.claude/skills/vercel-react-best-practices/rules/bundle-conditional.md +31 -0
- package/templates/.claude/skills/vercel-react-best-practices/rules/bundle-defer-third-party.md +49 -0
- package/templates/.claude/skills/vercel-react-best-practices/rules/bundle-dynamic-imports.md +35 -0
- package/templates/.claude/skills/vercel-react-best-practices/rules/bundle-preload.md +50 -0
- package/templates/.claude/skills/vercel-react-best-practices/rules/client-event-listeners.md +74 -0
- package/templates/.claude/skills/vercel-react-best-practices/rules/client-swr-dedup.md +56 -0
- package/templates/.claude/skills/vercel-react-best-practices/rules/js-batch-dom-css.md +82 -0
- package/templates/.claude/skills/vercel-react-best-practices/rules/js-cache-function-results.md +80 -0
- package/templates/.claude/skills/vercel-react-best-practices/rules/js-cache-property-access.md +28 -0
- package/templates/.claude/skills/vercel-react-best-practices/rules/js-cache-storage.md +70 -0
- package/templates/.claude/skills/vercel-react-best-practices/rules/js-combine-iterations.md +32 -0
- package/templates/.claude/skills/vercel-react-best-practices/rules/js-early-exit.md +50 -0
- package/templates/.claude/skills/vercel-react-best-practices/rules/js-hoist-regexp.md +45 -0
- package/templates/.claude/skills/vercel-react-best-practices/rules/js-index-maps.md +37 -0
- package/templates/.claude/skills/vercel-react-best-practices/rules/js-length-check-first.md +49 -0
- package/templates/.claude/skills/vercel-react-best-practices/rules/js-min-max-loop.md +82 -0
- package/templates/.claude/skills/vercel-react-best-practices/rules/js-set-map-lookups.md +24 -0
- package/templates/.claude/skills/vercel-react-best-practices/rules/js-tosorted-immutable.md +57 -0
- package/templates/.claude/skills/vercel-react-best-practices/rules/rendering-activity.md +26 -0
- package/templates/.claude/skills/vercel-react-best-practices/rules/rendering-animate-svg-wrapper.md +47 -0
- package/templates/.claude/skills/vercel-react-best-practices/rules/rendering-conditional-render.md +40 -0
- package/templates/.claude/skills/vercel-react-best-practices/rules/rendering-content-visibility.md +38 -0
- package/templates/.claude/skills/vercel-react-best-practices/rules/rendering-hoist-jsx.md +46 -0
- package/templates/.claude/skills/vercel-react-best-practices/rules/rendering-hydration-no-flicker.md +82 -0
- package/templates/.claude/skills/vercel-react-best-practices/rules/rendering-svg-precision.md +28 -0
- package/templates/.claude/skills/vercel-react-best-practices/rules/rerender-defer-reads.md +39 -0
- package/templates/.claude/skills/vercel-react-best-practices/rules/rerender-dependencies.md +45 -0
- package/templates/.claude/skills/vercel-react-best-practices/rules/rerender-derived-state.md +29 -0
- package/templates/.claude/skills/vercel-react-best-practices/rules/rerender-functional-setstate.md +74 -0
- package/templates/.claude/skills/vercel-react-best-practices/rules/rerender-lazy-state-init.md +58 -0
- package/templates/.claude/skills/vercel-react-best-practices/rules/rerender-memo.md +44 -0
- package/templates/.claude/skills/vercel-react-best-practices/rules/rerender-transitions.md +40 -0
- package/templates/.claude/skills/vercel-react-best-practices/rules/server-after-nonblocking.md +73 -0
- package/templates/.claude/skills/vercel-react-best-practices/rules/server-cache-lru.md +41 -0
- package/templates/.claude/skills/vercel-react-best-practices/rules/server-cache-react.md +26 -0
- package/templates/.claude/skills/vercel-react-best-practices/rules/server-parallel-fetching.md +79 -0
- package/templates/.claude/skills/vercel-react-best-practices/rules/server-serialization.md +38 -0
- package/templates/.claude/skills/vs-design-diverge/SKILL.md +307 -0
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Use Loop for Min/Max Instead of Sort
|
|
3
|
+
impact: LOW
|
|
4
|
+
impactDescription: O(n) instead of O(n log n)
|
|
5
|
+
tags: javascript, arrays, performance, sorting, algorithms
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Use Loop for Min/Max Instead of Sort
|
|
9
|
+
|
|
10
|
+
Finding the smallest or largest element only requires a single pass through the array. Sorting is wasteful and slower.
|
|
11
|
+
|
|
12
|
+
**Incorrect (O(n log n) - sort to find latest):**
|
|
13
|
+
|
|
14
|
+
```typescript
|
|
15
|
+
interface Project {
|
|
16
|
+
id: string
|
|
17
|
+
name: string
|
|
18
|
+
updatedAt: number
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function getLatestProject(projects: Project[]) {
|
|
22
|
+
const sorted = [...projects].sort((a, b) => b.updatedAt - a.updatedAt)
|
|
23
|
+
return sorted[0]
|
|
24
|
+
}
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Sorts the entire array just to find the maximum value.
|
|
28
|
+
|
|
29
|
+
**Incorrect (O(n log n) - sort for oldest and newest):**
|
|
30
|
+
|
|
31
|
+
```typescript
|
|
32
|
+
function getOldestAndNewest(projects: Project[]) {
|
|
33
|
+
const sorted = [...projects].sort((a, b) => a.updatedAt - b.updatedAt)
|
|
34
|
+
return { oldest: sorted[0], newest: sorted[sorted.length - 1] }
|
|
35
|
+
}
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Still sorts unnecessarily when only min/max are needed.
|
|
39
|
+
|
|
40
|
+
**Correct (O(n) - single loop):**
|
|
41
|
+
|
|
42
|
+
```typescript
|
|
43
|
+
function getLatestProject(projects: Project[]) {
|
|
44
|
+
if (projects.length === 0) return null
|
|
45
|
+
|
|
46
|
+
let latest = projects[0]
|
|
47
|
+
|
|
48
|
+
for (let i = 1; i < projects.length; i++) {
|
|
49
|
+
if (projects[i].updatedAt > latest.updatedAt) {
|
|
50
|
+
latest = projects[i]
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return latest
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function getOldestAndNewest(projects: Project[]) {
|
|
58
|
+
if (projects.length === 0) return { oldest: null, newest: null }
|
|
59
|
+
|
|
60
|
+
let oldest = projects[0]
|
|
61
|
+
let newest = projects[0]
|
|
62
|
+
|
|
63
|
+
for (let i = 1; i < projects.length; i++) {
|
|
64
|
+
if (projects[i].updatedAt < oldest.updatedAt) oldest = projects[i]
|
|
65
|
+
if (projects[i].updatedAt > newest.updatedAt) newest = projects[i]
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return { oldest, newest }
|
|
69
|
+
}
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Single pass through the array, no copying, no sorting.
|
|
73
|
+
|
|
74
|
+
**Alternative (Math.min/Math.max for small arrays):**
|
|
75
|
+
|
|
76
|
+
```typescript
|
|
77
|
+
const numbers = [5, 2, 8, 1, 9]
|
|
78
|
+
const min = Math.min(...numbers)
|
|
79
|
+
const max = Math.max(...numbers)
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
This works for small arrays but can be slower for very large arrays due to spread operator limitations. Use the loop approach for reliability.
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Use Set/Map for O(1) Lookups
|
|
3
|
+
impact: LOW-MEDIUM
|
|
4
|
+
impactDescription: O(n) to O(1)
|
|
5
|
+
tags: javascript, set, map, data-structures, performance
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Use Set/Map for O(1) Lookups
|
|
9
|
+
|
|
10
|
+
Convert arrays to Set/Map for repeated membership checks.
|
|
11
|
+
|
|
12
|
+
**Incorrect (O(n) per check):**
|
|
13
|
+
|
|
14
|
+
```typescript
|
|
15
|
+
const allowedIds = ['a', 'b', 'c', ...]
|
|
16
|
+
items.filter(item => allowedIds.includes(item.id))
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
**Correct (O(1) per check):**
|
|
20
|
+
|
|
21
|
+
```typescript
|
|
22
|
+
const allowedIds = new Set(['a', 'b', 'c', ...])
|
|
23
|
+
items.filter(item => allowedIds.has(item.id))
|
|
24
|
+
```
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Use toSorted() Instead of sort() for Immutability
|
|
3
|
+
impact: MEDIUM-HIGH
|
|
4
|
+
impactDescription: prevents mutation bugs in React state
|
|
5
|
+
tags: javascript, arrays, immutability, react, state, mutation
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Use toSorted() Instead of sort() for Immutability
|
|
9
|
+
|
|
10
|
+
`.sort()` mutates the array in place, which can cause bugs with React state and props. Use `.toSorted()` to create a new sorted array without mutation.
|
|
11
|
+
|
|
12
|
+
**Incorrect (mutates original array):**
|
|
13
|
+
|
|
14
|
+
```typescript
|
|
15
|
+
function UserList({ users }: { users: User[] }) {
|
|
16
|
+
// Mutates the users prop array!
|
|
17
|
+
const sorted = useMemo(
|
|
18
|
+
() => users.sort((a, b) => a.name.localeCompare(b.name)),
|
|
19
|
+
[users]
|
|
20
|
+
)
|
|
21
|
+
return <div>{sorted.map(renderUser)}</div>
|
|
22
|
+
}
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
**Correct (creates new array):**
|
|
26
|
+
|
|
27
|
+
```typescript
|
|
28
|
+
function UserList({ users }: { users: User[] }) {
|
|
29
|
+
// Creates new sorted array, original unchanged
|
|
30
|
+
const sorted = useMemo(
|
|
31
|
+
() => users.toSorted((a, b) => a.name.localeCompare(b.name)),
|
|
32
|
+
[users]
|
|
33
|
+
)
|
|
34
|
+
return <div>{sorted.map(renderUser)}</div>
|
|
35
|
+
}
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
**Why this matters in React:**
|
|
39
|
+
|
|
40
|
+
1. Props/state mutations break React's immutability model - React expects props and state to be treated as read-only
|
|
41
|
+
2. Causes stale closure bugs - Mutating arrays inside closures (callbacks, effects) can lead to unexpected behavior
|
|
42
|
+
|
|
43
|
+
**Browser support (fallback for older browsers):**
|
|
44
|
+
|
|
45
|
+
`.toSorted()` is available in all modern browsers (Chrome 110+, Safari 16+, Firefox 115+, Node.js 20+). For older environments, use spread operator:
|
|
46
|
+
|
|
47
|
+
```typescript
|
|
48
|
+
// Fallback for older browsers
|
|
49
|
+
const sorted = [...items].sort((a, b) => a.value - b.value)
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
**Other immutable array methods:**
|
|
53
|
+
|
|
54
|
+
- `.toSorted()` - immutable sort
|
|
55
|
+
- `.toReversed()` - immutable reverse
|
|
56
|
+
- `.toSpliced()` - immutable splice
|
|
57
|
+
- `.with()` - immutable element replacement
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Use Activity Component for Show/Hide
|
|
3
|
+
impact: MEDIUM
|
|
4
|
+
impactDescription: preserves state/DOM
|
|
5
|
+
tags: rendering, activity, visibility, state-preservation
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Use Activity Component for Show/Hide
|
|
9
|
+
|
|
10
|
+
Use React's `<Activity>` to preserve state/DOM for expensive components that frequently toggle visibility.
|
|
11
|
+
|
|
12
|
+
**Usage:**
|
|
13
|
+
|
|
14
|
+
```tsx
|
|
15
|
+
import { Activity } from 'react'
|
|
16
|
+
|
|
17
|
+
function Dropdown({ isOpen }: Props) {
|
|
18
|
+
return (
|
|
19
|
+
<Activity mode={isOpen ? 'visible' : 'hidden'}>
|
|
20
|
+
<ExpensiveMenu />
|
|
21
|
+
</Activity>
|
|
22
|
+
)
|
|
23
|
+
}
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Avoids expensive re-renders and state loss.
|
package/templates/.claude/skills/vercel-react-best-practices/rules/rendering-animate-svg-wrapper.md
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Animate SVG Wrapper Instead of SVG Element
|
|
3
|
+
impact: LOW
|
|
4
|
+
impactDescription: enables hardware acceleration
|
|
5
|
+
tags: rendering, svg, css, animation, performance
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Animate SVG Wrapper Instead of SVG Element
|
|
9
|
+
|
|
10
|
+
Many browsers don't have hardware acceleration for CSS3 animations on SVG elements. Wrap SVG in a `<div>` and animate the wrapper instead.
|
|
11
|
+
|
|
12
|
+
**Incorrect (animating SVG directly - no hardware acceleration):**
|
|
13
|
+
|
|
14
|
+
```tsx
|
|
15
|
+
function LoadingSpinner() {
|
|
16
|
+
return (
|
|
17
|
+
<svg
|
|
18
|
+
className="animate-spin"
|
|
19
|
+
width="24"
|
|
20
|
+
height="24"
|
|
21
|
+
viewBox="0 0 24 24"
|
|
22
|
+
>
|
|
23
|
+
<circle cx="12" cy="12" r="10" stroke="currentColor" />
|
|
24
|
+
</svg>
|
|
25
|
+
)
|
|
26
|
+
}
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
**Correct (animating wrapper div - hardware accelerated):**
|
|
30
|
+
|
|
31
|
+
```tsx
|
|
32
|
+
function LoadingSpinner() {
|
|
33
|
+
return (
|
|
34
|
+
<div className="animate-spin">
|
|
35
|
+
<svg
|
|
36
|
+
width="24"
|
|
37
|
+
height="24"
|
|
38
|
+
viewBox="0 0 24 24"
|
|
39
|
+
>
|
|
40
|
+
<circle cx="12" cy="12" r="10" stroke="currentColor" />
|
|
41
|
+
</svg>
|
|
42
|
+
</div>
|
|
43
|
+
)
|
|
44
|
+
}
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
This applies to all CSS transforms and transitions (`transform`, `opacity`, `translate`, `scale`, `rotate`). The wrapper div allows browsers to use GPU acceleration for smoother animations.
|
package/templates/.claude/skills/vercel-react-best-practices/rules/rendering-conditional-render.md
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Use Explicit Conditional Rendering
|
|
3
|
+
impact: LOW
|
|
4
|
+
impactDescription: prevents rendering 0 or NaN
|
|
5
|
+
tags: rendering, conditional, jsx, falsy-values
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Use Explicit Conditional Rendering
|
|
9
|
+
|
|
10
|
+
Use explicit ternary operators (`? :`) instead of `&&` for conditional rendering when the condition can be `0`, `NaN`, or other falsy values that render.
|
|
11
|
+
|
|
12
|
+
**Incorrect (renders "0" when count is 0):**
|
|
13
|
+
|
|
14
|
+
```tsx
|
|
15
|
+
function Badge({ count }: { count: number }) {
|
|
16
|
+
return (
|
|
17
|
+
<div>
|
|
18
|
+
{count && <span className="badge">{count}</span>}
|
|
19
|
+
</div>
|
|
20
|
+
)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// When count = 0, renders: <div>0</div>
|
|
24
|
+
// When count = 5, renders: <div><span class="badge">5</span></div>
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
**Correct (renders nothing when count is 0):**
|
|
28
|
+
|
|
29
|
+
```tsx
|
|
30
|
+
function Badge({ count }: { count: number }) {
|
|
31
|
+
return (
|
|
32
|
+
<div>
|
|
33
|
+
{count > 0 ? <span className="badge">{count}</span> : null}
|
|
34
|
+
</div>
|
|
35
|
+
)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// When count = 0, renders: <div></div>
|
|
39
|
+
// When count = 5, renders: <div><span class="badge">5</span></div>
|
|
40
|
+
```
|
package/templates/.claude/skills/vercel-react-best-practices/rules/rendering-content-visibility.md
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: CSS content-visibility for Long Lists
|
|
3
|
+
impact: HIGH
|
|
4
|
+
impactDescription: faster initial render
|
|
5
|
+
tags: rendering, css, content-visibility, long-lists
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## CSS content-visibility for Long Lists
|
|
9
|
+
|
|
10
|
+
Apply `content-visibility: auto` to defer off-screen rendering.
|
|
11
|
+
|
|
12
|
+
**CSS:**
|
|
13
|
+
|
|
14
|
+
```css
|
|
15
|
+
.message-item {
|
|
16
|
+
content-visibility: auto;
|
|
17
|
+
contain-intrinsic-size: 0 80px;
|
|
18
|
+
}
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
**Example:**
|
|
22
|
+
|
|
23
|
+
```tsx
|
|
24
|
+
function MessageList({ messages }: { messages: Message[] }) {
|
|
25
|
+
return (
|
|
26
|
+
<div className="overflow-y-auto h-screen">
|
|
27
|
+
{messages.map(msg => (
|
|
28
|
+
<div key={msg.id} className="message-item">
|
|
29
|
+
<Avatar user={msg.author} />
|
|
30
|
+
<div>{msg.content}</div>
|
|
31
|
+
</div>
|
|
32
|
+
))}
|
|
33
|
+
</div>
|
|
34
|
+
)
|
|
35
|
+
}
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
For 1000 messages, browser skips layout/paint for ~990 off-screen items (10× faster initial render).
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Hoist Static JSX Elements
|
|
3
|
+
impact: LOW
|
|
4
|
+
impactDescription: avoids re-creation
|
|
5
|
+
tags: rendering, jsx, static, optimization
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Hoist Static JSX Elements
|
|
9
|
+
|
|
10
|
+
Extract static JSX outside components to avoid re-creation.
|
|
11
|
+
|
|
12
|
+
**Incorrect (recreates element every render):**
|
|
13
|
+
|
|
14
|
+
```tsx
|
|
15
|
+
function LoadingSkeleton() {
|
|
16
|
+
return <div className="animate-pulse h-20 bg-gray-200" />
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function Container() {
|
|
20
|
+
return (
|
|
21
|
+
<div>
|
|
22
|
+
{loading && <LoadingSkeleton />}
|
|
23
|
+
</div>
|
|
24
|
+
)
|
|
25
|
+
}
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
**Correct (reuses same element):**
|
|
29
|
+
|
|
30
|
+
```tsx
|
|
31
|
+
const loadingSkeleton = (
|
|
32
|
+
<div className="animate-pulse h-20 bg-gray-200" />
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
function Container() {
|
|
36
|
+
return (
|
|
37
|
+
<div>
|
|
38
|
+
{loading && loadingSkeleton}
|
|
39
|
+
</div>
|
|
40
|
+
)
|
|
41
|
+
}
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
This is especially helpful for large and static SVG nodes, which can be expensive to recreate on every render.
|
|
45
|
+
|
|
46
|
+
**Note:** If your project has [React Compiler](https://react.dev/learn/react-compiler) enabled, the compiler automatically hoists static JSX elements and optimizes component re-renders, making manual hoisting unnecessary.
|
package/templates/.claude/skills/vercel-react-best-practices/rules/rendering-hydration-no-flicker.md
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Prevent Hydration Mismatch Without Flickering
|
|
3
|
+
impact: MEDIUM
|
|
4
|
+
impactDescription: avoids visual flicker and hydration errors
|
|
5
|
+
tags: rendering, ssr, hydration, localStorage, flicker
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Prevent Hydration Mismatch Without Flickering
|
|
9
|
+
|
|
10
|
+
When rendering content that depends on client-side storage (localStorage, cookies), avoid both SSR breakage and post-hydration flickering by injecting a synchronous script that updates the DOM before React hydrates.
|
|
11
|
+
|
|
12
|
+
**Incorrect (breaks SSR):**
|
|
13
|
+
|
|
14
|
+
```tsx
|
|
15
|
+
function ThemeWrapper({ children }: { children: ReactNode }) {
|
|
16
|
+
// localStorage is not available on server - throws error
|
|
17
|
+
const theme = localStorage.getItem('theme') || 'light'
|
|
18
|
+
|
|
19
|
+
return (
|
|
20
|
+
<div className={theme}>
|
|
21
|
+
{children}
|
|
22
|
+
</div>
|
|
23
|
+
)
|
|
24
|
+
}
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Server-side rendering will fail because `localStorage` is undefined.
|
|
28
|
+
|
|
29
|
+
**Incorrect (visual flickering):**
|
|
30
|
+
|
|
31
|
+
```tsx
|
|
32
|
+
function ThemeWrapper({ children }: { children: ReactNode }) {
|
|
33
|
+
const [theme, setTheme] = useState('light')
|
|
34
|
+
|
|
35
|
+
useEffect(() => {
|
|
36
|
+
// Runs after hydration - causes visible flash
|
|
37
|
+
const stored = localStorage.getItem('theme')
|
|
38
|
+
if (stored) {
|
|
39
|
+
setTheme(stored)
|
|
40
|
+
}
|
|
41
|
+
}, [])
|
|
42
|
+
|
|
43
|
+
return (
|
|
44
|
+
<div className={theme}>
|
|
45
|
+
{children}
|
|
46
|
+
</div>
|
|
47
|
+
)
|
|
48
|
+
}
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Component first renders with default value (`light`), then updates after hydration, causing a visible flash of incorrect content.
|
|
52
|
+
|
|
53
|
+
**Correct (no flicker, no hydration mismatch):**
|
|
54
|
+
|
|
55
|
+
```tsx
|
|
56
|
+
function ThemeWrapper({ children }: { children: ReactNode }) {
|
|
57
|
+
return (
|
|
58
|
+
<>
|
|
59
|
+
<div id="theme-wrapper">
|
|
60
|
+
{children}
|
|
61
|
+
</div>
|
|
62
|
+
<script
|
|
63
|
+
dangerouslySetInnerHTML={{
|
|
64
|
+
__html: `
|
|
65
|
+
(function() {
|
|
66
|
+
try {
|
|
67
|
+
var theme = localStorage.getItem('theme') || 'light';
|
|
68
|
+
var el = document.getElementById('theme-wrapper');
|
|
69
|
+
if (el) el.className = theme;
|
|
70
|
+
} catch (e) {}
|
|
71
|
+
})();
|
|
72
|
+
`,
|
|
73
|
+
}}
|
|
74
|
+
/>
|
|
75
|
+
</>
|
|
76
|
+
)
|
|
77
|
+
}
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
The inline script executes synchronously before showing the element, ensuring the DOM already has the correct value. No flickering, no hydration mismatch.
|
|
81
|
+
|
|
82
|
+
This pattern is especially useful for theme toggles, user preferences, authentication states, and any client-only data that should render immediately without flashing default values.
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Optimize SVG Precision
|
|
3
|
+
impact: LOW
|
|
4
|
+
impactDescription: reduces file size
|
|
5
|
+
tags: rendering, svg, optimization, svgo
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Optimize SVG Precision
|
|
9
|
+
|
|
10
|
+
Reduce SVG coordinate precision to decrease file size. The optimal precision depends on the viewBox size, but in general reducing precision should be considered.
|
|
11
|
+
|
|
12
|
+
**Incorrect (excessive precision):**
|
|
13
|
+
|
|
14
|
+
```svg
|
|
15
|
+
<path d="M 10.293847 20.847362 L 30.938472 40.192837" />
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
**Correct (1 decimal place):**
|
|
19
|
+
|
|
20
|
+
```svg
|
|
21
|
+
<path d="M 10.3 20.8 L 30.9 40.2" />
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
**Automate with SVGO:**
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
npx svgo --precision=1 --multipass icon.svg
|
|
28
|
+
```
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Defer State Reads to Usage Point
|
|
3
|
+
impact: MEDIUM
|
|
4
|
+
impactDescription: avoids unnecessary subscriptions
|
|
5
|
+
tags: rerender, searchParams, localStorage, optimization
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Defer State Reads to Usage Point
|
|
9
|
+
|
|
10
|
+
Don't subscribe to dynamic state (searchParams, localStorage) if you only read it inside callbacks.
|
|
11
|
+
|
|
12
|
+
**Incorrect (subscribes to all searchParams changes):**
|
|
13
|
+
|
|
14
|
+
```tsx
|
|
15
|
+
function ShareButton({ chatId }: { chatId: string }) {
|
|
16
|
+
const searchParams = useSearchParams()
|
|
17
|
+
|
|
18
|
+
const handleShare = () => {
|
|
19
|
+
const ref = searchParams.get('ref')
|
|
20
|
+
shareChat(chatId, { ref })
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return <button onClick={handleShare}>Share</button>
|
|
24
|
+
}
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
**Correct (reads on demand, no subscription):**
|
|
28
|
+
|
|
29
|
+
```tsx
|
|
30
|
+
function ShareButton({ chatId }: { chatId: string }) {
|
|
31
|
+
const handleShare = () => {
|
|
32
|
+
const params = new URLSearchParams(window.location.search)
|
|
33
|
+
const ref = params.get('ref')
|
|
34
|
+
shareChat(chatId, { ref })
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return <button onClick={handleShare}>Share</button>
|
|
38
|
+
}
|
|
39
|
+
```
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Narrow Effect Dependencies
|
|
3
|
+
impact: LOW
|
|
4
|
+
impactDescription: minimizes effect re-runs
|
|
5
|
+
tags: rerender, useEffect, dependencies, optimization
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Narrow Effect Dependencies
|
|
9
|
+
|
|
10
|
+
Specify primitive dependencies instead of objects to minimize effect re-runs.
|
|
11
|
+
|
|
12
|
+
**Incorrect (re-runs on any user field change):**
|
|
13
|
+
|
|
14
|
+
```tsx
|
|
15
|
+
useEffect(() => {
|
|
16
|
+
console.log(user.id)
|
|
17
|
+
}, [user])
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
**Correct (re-runs only when id changes):**
|
|
21
|
+
|
|
22
|
+
```tsx
|
|
23
|
+
useEffect(() => {
|
|
24
|
+
console.log(user.id)
|
|
25
|
+
}, [user.id])
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
**For derived state, compute outside effect:**
|
|
29
|
+
|
|
30
|
+
```tsx
|
|
31
|
+
// Incorrect: runs on width=767, 766, 765...
|
|
32
|
+
useEffect(() => {
|
|
33
|
+
if (width < 768) {
|
|
34
|
+
enableMobileMode()
|
|
35
|
+
}
|
|
36
|
+
}, [width])
|
|
37
|
+
|
|
38
|
+
// Correct: runs only on boolean transition
|
|
39
|
+
const isMobile = width < 768
|
|
40
|
+
useEffect(() => {
|
|
41
|
+
if (isMobile) {
|
|
42
|
+
enableMobileMode()
|
|
43
|
+
}
|
|
44
|
+
}, [isMobile])
|
|
45
|
+
```
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Subscribe to Derived State
|
|
3
|
+
impact: MEDIUM
|
|
4
|
+
impactDescription: reduces re-render frequency
|
|
5
|
+
tags: rerender, derived-state, media-query, optimization
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Subscribe to Derived State
|
|
9
|
+
|
|
10
|
+
Subscribe to derived boolean state instead of continuous values to reduce re-render frequency.
|
|
11
|
+
|
|
12
|
+
**Incorrect (re-renders on every pixel change):**
|
|
13
|
+
|
|
14
|
+
```tsx
|
|
15
|
+
function Sidebar() {
|
|
16
|
+
const width = useWindowWidth() // updates continuously
|
|
17
|
+
const isMobile = width < 768
|
|
18
|
+
return <nav className={isMobile ? 'mobile' : 'desktop'}>
|
|
19
|
+
}
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
**Correct (re-renders only when boolean changes):**
|
|
23
|
+
|
|
24
|
+
```tsx
|
|
25
|
+
function Sidebar() {
|
|
26
|
+
const isMobile = useMediaQuery('(max-width: 767px)')
|
|
27
|
+
return <nav className={isMobile ? 'mobile' : 'desktop'}>
|
|
28
|
+
}
|
|
29
|
+
```
|
package/templates/.claude/skills/vercel-react-best-practices/rules/rerender-functional-setstate.md
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Use Functional setState Updates
|
|
3
|
+
impact: MEDIUM
|
|
4
|
+
impactDescription: prevents stale closures and unnecessary callback recreations
|
|
5
|
+
tags: react, hooks, useState, useCallback, callbacks, closures
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Use Functional setState Updates
|
|
9
|
+
|
|
10
|
+
When updating state based on the current state value, use the functional update form of setState instead of directly referencing the state variable. This prevents stale closures, eliminates unnecessary dependencies, and creates stable callback references.
|
|
11
|
+
|
|
12
|
+
**Incorrect (requires state as dependency):**
|
|
13
|
+
|
|
14
|
+
```tsx
|
|
15
|
+
function TodoList() {
|
|
16
|
+
const [items, setItems] = useState(initialItems)
|
|
17
|
+
|
|
18
|
+
// Callback must depend on items, recreated on every items change
|
|
19
|
+
const addItems = useCallback((newItems: Item[]) => {
|
|
20
|
+
setItems([...items, ...newItems])
|
|
21
|
+
}, [items]) // ❌ items dependency causes recreations
|
|
22
|
+
|
|
23
|
+
// Risk of stale closure if dependency is forgotten
|
|
24
|
+
const removeItem = useCallback((id: string) => {
|
|
25
|
+
setItems(items.filter(item => item.id !== id))
|
|
26
|
+
}, []) // ❌ Missing items dependency - will use stale items!
|
|
27
|
+
|
|
28
|
+
return <ItemsEditor items={items} onAdd={addItems} onRemove={removeItem} />
|
|
29
|
+
}
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
The first callback is recreated every time `items` changes, which can cause child components to re-render unnecessarily. The second callback has a stale closure bug—it will always reference the initial `items` value.
|
|
33
|
+
|
|
34
|
+
**Correct (stable callbacks, no stale closures):**
|
|
35
|
+
|
|
36
|
+
```tsx
|
|
37
|
+
function TodoList() {
|
|
38
|
+
const [items, setItems] = useState(initialItems)
|
|
39
|
+
|
|
40
|
+
// Stable callback, never recreated
|
|
41
|
+
const addItems = useCallback((newItems: Item[]) => {
|
|
42
|
+
setItems(curr => [...curr, ...newItems])
|
|
43
|
+
}, []) // ✅ No dependencies needed
|
|
44
|
+
|
|
45
|
+
// Always uses latest state, no stale closure risk
|
|
46
|
+
const removeItem = useCallback((id: string) => {
|
|
47
|
+
setItems(curr => curr.filter(item => item.id !== id))
|
|
48
|
+
}, []) // ✅ Safe and stable
|
|
49
|
+
|
|
50
|
+
return <ItemsEditor items={items} onAdd={addItems} onRemove={removeItem} />
|
|
51
|
+
}
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
**Benefits:**
|
|
55
|
+
|
|
56
|
+
1. **Stable callback references** - Callbacks don't need to be recreated when state changes
|
|
57
|
+
2. **No stale closures** - Always operates on the latest state value
|
|
58
|
+
3. **Fewer dependencies** - Simplifies dependency arrays and reduces memory leaks
|
|
59
|
+
4. **Prevents bugs** - Eliminates the most common source of React closure bugs
|
|
60
|
+
|
|
61
|
+
**When to use functional updates:**
|
|
62
|
+
|
|
63
|
+
- Any setState that depends on the current state value
|
|
64
|
+
- Inside useCallback/useMemo when state is needed
|
|
65
|
+
- Event handlers that reference state
|
|
66
|
+
- Async operations that update state
|
|
67
|
+
|
|
68
|
+
**When direct updates are fine:**
|
|
69
|
+
|
|
70
|
+
- Setting state to a static value: `setCount(0)`
|
|
71
|
+
- Setting state from props/arguments only: `setName(newName)`
|
|
72
|
+
- State doesn't depend on previous value
|
|
73
|
+
|
|
74
|
+
**Note:** If your project has [React Compiler](https://react.dev/learn/react-compiler) enabled, the compiler can automatically optimize some cases, but functional updates are still recommended for correctness and to prevent stale closure bugs.
|