| paths |
|---|
**/*.{ts,tsx} |
- Purity & Architecture
- Render must be pure/deterministic: same inputs → same JSX.
- Must be StrictMode-safe: no side effects in render (no mutations/API calls).
- Keep render logic separate from event handlers.
- State: Single Source + Minimal
- Avoid redundant state; derive during render when possible (e.g., fullName).
- Don’t mirror props into state unless you intentionally ignore future prop updates.
- Group related state to avoid impossible combinations.
- Use
keyto reset state across distinct entities; don’tuseEffect-reset for identity changes.
- Effects = Escape Hatch
useEffectis for syncing with external systems, not internal calculations/transformations.- Dependencies must be honest: anything referenced inside goes in the deps array.
- If it can be done in render or an event handler, don’t use an effect.
- Async & Races
useEffectfetching must guard races via cleanup (AbortControlleror ignore-flag).- Use functional updates when next state depends on previous (
setX(prev => ...)). - Prefer React Query / SWR over manual effect-fetching for caching/deduping/race handling.
- Hooks & Extraction
- Custom hooks expose intent (what), hide implementation (how).
- Use
useReffor persistent non-reactive values (timers, previous values). - Use
useCallback/useMemomainly for stable identity across deps/children.
- Quality Rules
- Props/state are immutable; update via copies (spread / immut helpers).
- Use guard clauses / early returns to reduce nesting and clarify render flow.