mirror of
https://github.com/kjanat/livedash-node.git
synced 2026-02-13 14:15:44 +01:00
- Fix syntax errors in skills markdown files (.github/skills, .opencode/skills) - Change typescript to tsx for code blocks with JSX - Replace ellipsis (...) in array examples with valid syntax - Separate CSS from TypeScript into distinct code blocks - Convert JavaScript object examples to valid JSON in docs - Fix enum definitions with proper comma separation
46 lines
833 B
Markdown
46 lines
833 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]);
|
|
```
|