Rxova
Skip to content

useSharedState

function useSharedState<T>(
key,
initial,
options?): [T, (next) => void];

Like useState, but the value exists in every tab, window, and worker on this origin. Late-joining tabs hydrate to the current value; concurrent writes converge last-writer-wins. Pass options.scope to delimit how much is shared (‘everywhere’ | ‘tabs’ | ‘tab’).

Note the convergence rule is last-writer-wins per key, not per operation: two tabs running set(n => n + 1) at the same moment agree on one value, and one of the increments is lost. For counters and other accumulating writes, drive them from a single tab (useLeaderEffect) until an op-based primitive lands.

Type Parameter
T
ParameterType
keystring
initialT
options?UseSharedStateOptions

[T, (next) => void]