React Integration
TheuseSystem hook connects your React components to an Idyllic system. It establishes a WebSocket connection, subscribes to state updates, and returns typed state and actions. When server-side state changes, your component re-renders automatically.
<DocImprover> tells TypeScript which system you’re connecting to. This enables the framework to infer types for state and actions from your system definition—full autocompletion and type checking without client-side type definitions.
What useSystem Returns
The hook returns an object containing your system’s state properties and action methods, plus connection status:count, tasks) are reactive—reading them causes re-renders when they change. Action methods (generate) forward calls to the server over WebSocket. The status property indicates connection state, and error contains failure details if applicable.
| Property | Description |
|---|---|
| State fields | Reactive values that trigger re-renders on change |
| Actions | Typed methods that execute on the server |
status | 'connecting' | 'connected' | 'disconnected' | 'error' |
error | Error object if connection failed |
Reading State
State values read like regular JavaScript properties. The framework tracks which properties your component accesses and re-renders when they change:Stream Values
For streaming content like LLM output, use thecurrent property to display accumulated content and status to show indicators:
Calling Actions
Actions are typed proxies that forward calls to the server. Call them like regular async functions:Fire and Forget
For streaming operations, trigger the action without awaiting. The server streams chunks into state, and your component re-renders as that state updates:Connection Options
Pass options to configure the connection:Instance IDs
Theid option determines which system instance you connect to. Each unique ID corresponds to a separate Durable Object with isolated state:
Connection Status
Handle different connection states in your UI:'connecting' → 'connected' on success. If connection drops, 'disconnected' while reconnecting. If reconnection fails, 'error'.
Multiple Systems
Connect to multiple systems in one component. Each creates an independent WebSocket:Type Generation
Types generate automatically during development:.idyllic/types.ts. Import your system type and pass it to useSystem:
FAQ
When does useSystem create vs connect?
The hook connects to an existing instance or creates one on-demand. There’s no explicit “create”—instances materialize when the first client connects with a given ID.What happens when the component unmounts?
The WebSocket closes. If the user navigates back, a new connection opens and receives a fresh state snapshot. Server-side execution continues regardless of client connections.How do I share state between components?
Two options: (1) Use the same instance ID in multiple components—each gets its own connection but sees identical state. (2) LiftuseSystem to a parent and pass state via props or context.
How do I test components?
Mock the hook to provide controlled state and action spies:npx idyllic dev and use actual connections.
Guides
Learn patterns for building with Idyllic