Dot-Notation & Bracket Selectors
SuperState's selectors allow you to subscribe to specific nested properties or array indices, making your components high-performance and focused.
#How It Works
If you have a complex state object like user: { profile: { location: 'PK' } }, you can subscribe to any level deep by using a dot-separated string.
ts
LocationBadge.tsx1import { useSuperState } from "react-use-superstate"
2
3export function LocationBadge() {
4 // Ultra-fine subscription: only re-renders if location changes.
5 const [location, setLocation] = useSuperState('user.profile.location')
6
7 return <span className="p-2 bg-muted rounded">{location}</span>
8}
#Array Index Access
Use bracket notation to subscribe to specific array indices. Only the component watching that index will re-render when it changes.
ts
FirstItem.tsx1import { useSuperState } from "react-use-superstate"
2
3export function FirstItem() {
4 // Only re-renders if food[0] changes — not food[1], food[2], etc.
5 const [first, setFirst] = useSuperState('food[0]')
6
7 return <span>{first}</span>
8}
9
10// Deep array + object access also works:
11const [name] = useSuperState('users[2].name')
#Updating Deep State
The setLocation function returned by useSuperState('user.profile.location') will only update that nested key and notify other subscribers of the parent keys.
ts
Settings.tsx1// Parent subscribers: user, user.profile
2// Property subscribers: user.profile.location
3const [location, setLocation] = useSuperState('user.profile.location')
4
5const update = () => setLocation('US') // Correctly notifies all relevant watchers.
Type Safety Tip
SuperState's selector notation is dynamic. When using deep keys, TypeScript cannot always infer the exact nested type automatically. We recommend using a generic type parameter:
useSuperState<string>('user.name').