State Owners
In SuperState, state is not created in a vacuum. Every piece of global state is "owned" by the component that initializes it.
#The Owner Pattern
The component that calls createSuperState with a specific key becomes the owner. If multiple components try to own the same key, they will all stay in sync, but typically you define the source of truth in a high-level layout or a specific feature container.
ts
SettingsSection.tsx1import { createSuperState } from "react-use-superstate"
2
3export function SettingsSection() {
4 // This component 'owns' the settings key
5 const [settings, setSettings] = createSuperState('settings', {
6 theme: 'dark',
7 notifications: true
8 })
9
10 return (
11 <div>
12 <h3>Set Theme</h3>
13 <button onClick={() => setSettings(s => ({ ...s, theme: 'light' }))}>
14 Switch to Light
15 </button>
16 </div>
17 )
18}
#Lifecycle & Persistence
Even if the owner component unmounts, the global state remains in the external store. However, the *initial value* provided by the owner acts as the default if the state is ever reset.
Decoupled Logic
Ownership is about initialization. Once initialized, any component can update the state without the owner needing to be present in the render tree.
NextState Consumers