State Management
Remix components manage state with plain JavaScript variables. Callhandle.update() to trigger re-renders.
Basic State
State is stored in the setup scope and persists for the component’s lifetime:Initializing State from Setup Prop
Use thesetup prop to initialize state:
Best Practices
1. Use Minimal Component State
Only store state needed for rendering. Derive computed values instead of storing them:2. Do Work in Event Handlers
Keep transient state in event handler scope. Only store in component state if needed for rendering:3. Don’t Store Input State You Don’t Need
Read input values directly from the DOM when possible:Controlled vs Uncontrolled Inputs
Uncontrolled Inputs
Use when only the user controls the value:Controlled Inputs
Use when the value can be set programmatically:Async State and Loading
Useawait handle.update() to show loading states:
Data Loading Patterns
Event Handler Pattern
Load data in response to user events:Reactive Loading with queueTask
Load data that responds to prop changes:Initial Data Loading
Load data once in the setup scope:Managing Cleanup with Signals
Usehandle.signal for cleanup when the component disconnects:
Global Event Listeners
Usehandle.on() for automatic cleanup: