useSuperState API
The consumer hook for accessing and updating existing global state. It allows any component to subscribe to state updates via a unique string key.
#Signature
ts
app/page.tsx1// Basic subscription
2const [state, setState] = useSuperState(key: string)
3
4// Subscribing to nested properties (Dot-notation)
5const [city, setCity] = useSuperState('user.address.city')
#TypeScript
Because SuperState is completely decoupled and relies on string keys, you must pass a generic type to useSuperState to prevent TypeScript from treating the state as unknown.
ts
TypeScript Example1// Prevents "Type 'unknown' is not assignable to type 'ReactNode'" errors
2const [age] = useSuperState<number>('user.age')
3const [name] = useSuperState<string>('user.name')
#Parameters
key (required)The unique string key of the global state you want to consume. This must match the key used in
createSuperState.
#Nested Updates
SuperState performs an immutable path update when you update a nested property. It shallow-clones each level of the object tree along the path, preserving references to unchanged branches. Updates are atomic and only notify components watching relevant keys.
ts
NestedUpdate.tsx1const [city, setCity] = useSuperState('user.address.city')
2
3// Updating just the city property
4const updateCity = () => setCity('London')
Fine-grained Reactivity
Components using
useSuperState('user.address.city') will not re-render if user.name changes.Previous createSuperState