Overview
store is useState at module scope: call store(initialObject) once to create a shared reactive bag, then call the returned hook inside any component to get [wrapper, update].
Every component that reads a property of the wrapper subscribes to re-render when that state changes. Methods, direct assignment, and update() follow the same rules as useState — mutations from one component are visible in all others immediately.
Keep stores domain-focused (authStore, cartStore, uiStore). For parent→child UI plumbing without globals, prefer scope() + getParentScope().
Compared to useState
|
API
|
Scope
|
Notes
|
|---|---|---|
useState(obj)
| component-local | Each component instance gets its own independent state. |
store(obj)
| module-global | One shared object; the factory returns a hook for any component. |
store — shared global state
Both Counter and StatsPanel call counterStore() independently, yet they share the exact same state. A mutation from Counter is instantly visible in StatsPanel — no props or context plumbing required.
Methods and update()
Auto-wrapped methods mutate shared prefs; update(fn) batches structural edits in one notify — same API as useState, shared across the app.