Decision Tree
useWidgetState
Persistent state that survives re-renders and display mode changes. Unlike React’suseState, this state is stored by the host and restored when your widget remounts.
API
state: Current state (ornullif not yet initialized)setState: Update function (accepts value or updater function)defaultState: Initial state (used if no persisted state exists)
Patterns
Form state:createStore
A Zustand store that automatically syncs with the host’s persistent state. UnlikeuseWidgetState, this provides actions, computed values, and middleware support for complex state management.
Automatic Persistence
createStore automatically:
- Syncs state to the host’s persistent storage
- Restores state from the host on load
- Filters out functions (actions) during serialization
Comparison Table
| Feature | useWidgetState | createStore |
|---|---|---|
| Setup complexity | Simple | Moderate |
| State shape | Any object | Object with actions |
| Multiple consumers | Re-renders all | Selective subscriptions |
| Computed values | Manual | Built-in with get() |
| Async actions | Manual with callbacks | Built-in |
| Best for | Simple forms, flags | Shopping carts, complex flows |
Combining with data-llm
State management is separate from LLM context. Use both when needed:useWidgetStatepersists the selectiondata-llmtells the model what the user sees
When State Persists
Widget state persists across re-renders, re-mounts, and display mode changes. Skybridge handles the storage mechanism automatically. Widget state resets when:- A new conversation starts
- The tool is called again with new input
- The user explicitly clears it
Migration from useState
If you’re using ReactuseState and losing state on re-renders:
useState for easy migration.
Related
- useWidgetState API
- createStore API
- LLM Context Sync
- MCP Apps - MCP Apps limitations
- Apps SDK (ChatGPT) - Apps SDK features