mirror of
https://github.com/NicholaiVogel/dashore-incubator.git
synced 2026-04-24 08:48:43 +00:00
add web-perf and vercel-react-best-practices skills from skills.sh to improve ai assistance for this next.js + cloudflare workers project. - web-perf: core web vitals analysis and performance auditing - vercel-react-best-practices: react/next.js optimization patterns - wrangler: cloudflare workers cli guidance (already existed)
46 lines
824 B
Markdown
46 lines
824 B
Markdown
---
|
|
title: Narrow Effect Dependencies
|
|
impact: LOW
|
|
impactDescription: minimizes effect re-runs
|
|
tags: rerender, useEffect, dependencies, optimization
|
|
---
|
|
|
|
## Narrow Effect Dependencies
|
|
|
|
Specify primitive dependencies instead of objects to minimize effect re-runs.
|
|
|
|
**Incorrect (re-runs on any user field change):**
|
|
|
|
```tsx
|
|
useEffect(() => {
|
|
console.log(user.id)
|
|
}, [user])
|
|
```
|
|
|
|
**Correct (re-runs only when id changes):**
|
|
|
|
```tsx
|
|
useEffect(() => {
|
|
console.log(user.id)
|
|
}, [user.id])
|
|
```
|
|
|
|
**For derived state, compute outside effect:**
|
|
|
|
```tsx
|
|
// Incorrect: runs on width=767, 766, 765...
|
|
useEffect(() => {
|
|
if (width < 768) {
|
|
enableMobileMode()
|
|
}
|
|
}, [width])
|
|
|
|
// Correct: runs only on boolean transition
|
|
const isMobile = width < 768
|
|
useEffect(() => {
|
|
if (isMobile) {
|
|
enableMobileMode()
|
|
}
|
|
}, [isMobile])
|
|
```
|