notque/vexjoy-agent / skills/frontend/frontend
frontend
UnverifiedFrontend: UI design, distinctive visual styles, HTML artifacts, Three.js 3D, TypeScript/React/Next.js apps, e-commerce, portfolios, React Native, web performance.
references/react-native/state-management.md
View on GitHub# State Management Reference
<!-- Loaded via react-native.md when task involves useState, derived state, Zustand, state structure, dispatchers, ground truth -->
## Use Dispatch Updaters When Next State Depends on Current
**Impact:** MEDIUM — prevents stale closures
**Instead of:**
```tsx
const onLayout = (e: LayoutChangeEvent) => {
const { width, height } = e.nativeEvent.layout
if (size?.width !== width || size?.height !== height) setSize({ width, height })
}
```
**Use:**
```tsx
const onLayout = (e: LayoutChangeEvent) => {
const { width, height } = e.nativeEvent.layout
setSize((prev) => {
if (prev?.width === width && prev?.height === height) return prev
return { width, height }
})
}
```
For simple cases: `setCount((prev) => prev + 1)` instead of `setCount(count + 1)`.
Not needed for primitive state set to a new value directly.
---
## Use Fallback State — undefined Means "User Hasn't Chosen Yet"
**Impact:** MEDIUM — reactive fallbacks, no stale initialState
**Instead of:**
```tsx
const [enabled, setEnabled] = useState(defaultEnabled) // locks at mount
```
**Use:**
```tsx
const [_enabled, setEnabled] = useState<boolean | undefined>(undefined)
const enabled = _enabled ?? defaultEnabled
// undefined = user hasn't touched it — falls back to parent prop
// Once user interacts, their choice persists
```
With server data:
```tsx
const [_theme, setTheme] = useState<string | undefined>(undefined)
const theme = _theme ?? data.theme
```
---
## Minimize State — Derive Values During Render
**Impact:** MEDIUM — fewer re-renders, no state drift
**Instead of:**
```tsx
const [total, setTotal] = useState(0)
useEffect(() => { setTotal(items.reduce(...)) }, [items])
```
**Use:**
```tsx
const total = items.reduce((sum, item) => sum + item.price, 0)
const itemCount = items.length
```
State is the minimal source of truth. Everything else is derived.
---
## State Must Represent Ground Truth
**Impact:** HIGH — single source of truth, easier debugging
State variables represent what is happening (`pressed`, `progress`, `isOpen`), not derived visuals (`scale`, `opacity`, `height`). Derive visuals from state.
**Instead of:**
```tsx
const [isExpanded, setIsExpanded] = useState(false)
const [height, setHeight] = useState(0)
useEffect(() => { setHeight(isExpanded ? 200 : 0) }, [isExpanded])
```
**Use:**
```tsx
const [isExpanded, setIsExpanded] = useState(false)
const height = isExpanded ? 200 : 0
```
Same principle applies to Reanimated shared values — see `animation-patterns.md`.
---
## Track Scroll Position in Shared Values or Refs
**Impact:** HIGH — prevents render thrashing during scroll
Scroll events fire 60/s. `useState` for scroll position = re-render every frame = dropped frames.
**Instead of:**
```tsx
const [scrollY, setScrollY] = useState(0)
const onScroll = (e) => setScrollY(e.nativeEvent.contentOffset.y)
```
**Use (Reanimated for scroll-driven animations):**
```tsx
const scrollY = useSharedValue(0)
const onScroll = useAnimatedScrollHandler({
onScroll: (e) => { scrollY.value = e.contentOffset.y }
})
return <Animated.ScrollView onScroll={onScroll} scrollEventThrottle={16} />
```
**Use (ref for non-reactive tracking):**
```tsx
const scrollY = useRef(0)
const onScroll = (e) => { scrollY.current = e.nativeEvent.contentOffset.y }
```
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| brainstormingobra/superpowersYou MUST use this before any creative work - creating features, building components, adding functionality, modifying behavior, or planning anything new, in software or out of it (a talk, a business, a renovation). | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| brand-guidelinesanthropics/skillsApplies Anthropic's official brand colors and typography to any sort of artifact that may benefit from having Anthropic's look-and-feel. Use it when brand colors or style guidelines, visual formatting, or company design standards apply. | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |
| frontend-designanthropics/skillsGuidance for distinctive, intentional visual design when building new UI or reshaping an existing one. Helps with aesthetic direction, typography, and making choices that don't read as templated defaults. | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |
| mcp-builderanthropics/skillsGuide for creating high-quality MCP (Model Context Protocol) servers that enable LLMs to interact with external services through well-designed tools. Use when building MCP servers to integrate external APIs or services, whether in Python (FastMCP) or Node/TypeScript (MCP SDK). | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |