Component State Management
Guide for managing local component state with React's built-in state management
Component State Management
Overview
This document outlines our approach to managing component-level state in our React Native application. Component state represents local state that is specific to a single component or a small component tree. We implement React's built-in state management hooks (useState and useReducer) for managing ephemeral UI state, form data, and other localized concerns. This follows our application's Golden Rule: "Component state belongs in React's built-in hooks, not in Zustand or TanStack Query". This approach optimizes for simplicity, performance, and proper separation of concerns.
Purpose & Scope
- Target Audience: React Native developers implementing UI components that require local state management
- Problems Addressed: Proper state isolation, form management, UI interactions, and temporary state that doesn't belong in global stores
- Scope Boundaries: Covers React's built-in state management hooks, common patterns, performance optimization, and custom hooks, but does not cover global state or server data management
Core Components/Architecture
When to Use Component State
Use React's built-in state management for:
- Form inputs before submission
- UI toggles (dropdowns, modals specific to one component)
- Temporary values during user interaction
- Animation states
- Local component configuration
- Any state that doesn't need to be shared beyond a small component tree
Basic Component State
Simple State with useState
The useState hook is ideal for managing simple, independent state values in a component.
When to use useState:
- For independent state variables
- When state logic is simple
- For primitive values or simple objects
- When you don't need derived state
Temporary UI State
State that's only needed during specific user interactions like loading, confirmation, or error states.
Common Temporary State Types:
- Loading indicators
- Validation states
- Confirmation dialogs
- Error messages
- Success notifications
Common Patterns
Controlled Components
Controlled components are React components where form data is handled by the component's state.
Benefits:
- Predictable flow of data
- Single source of truth
- Easier to validate or transform input
- Enables immediate UI responses to user input
Collapsible UI Elements
Toggle-based UI elements like accordions, expandable panels, and dropdown menus are common patterns that use local state.
Implementation Notes:
- Keep toggle state close to where it's used
- For multiple accordion items, consider an array of expanded states or IDs
- For complex cases, useReducer may be more appropriate
Custom Hooks for Reusable Component State
Toggle State Hook
A simple hook for boolean toggle state that's commonly used in UI components.
Usage Example
When to Elevate State
Performance Considerations
1. Minimize Re-renders
2. Use Callbacks Appropriately
Testing Component State
Troubleshooting
Related Documents
State Management Overview
Overview of our complete state management approach including client, server, and component state.
Client State Management
Companion documentation for handling global client state with Zustand.
Server State Management
Companion documentation for handling server state with TanStack Query.