State Management Best Practices
Core Principles
1. Choose the Right State Type
const [isModalOpen, setIsModalOpen] = useState(false);
const [selectedTab, setSelectedTab] = useState('items');const frameRef = useRef<number>(0);
const previousValueRef = useRef<number>(0);2. Avoid Stale Closures
3. Memoize Expensive Computations
4. Clean Up Side Effects
5. Batch State Updates
6. Functional Updates for Dependent State
7. Separate Concerns: Refs for Animation, State for UI
Common Patterns
Pattern: Animation Controller with Skip
Pattern: Tracking Selection in Lists
Pattern: Conditional State Updates
Debugging Tips
1. Track why components re-render
2. Detect stale closures
3. Verify ref updates
Performance Optimization
Use React.memo for expensive components
Avoid inline function creation in render
Use keys properly in lists
See Also
Last updated