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 Parameters
Section titled “Type Parameters”| Type Parameter |
|---|
T |
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
key | string |
initial | T |
options? | UseSharedStateOptions |
Returns
Section titled “Returns”[T, (next) => void]