globalAtomFamily

Signature
globalAtomFamily<T, K>(defaultFactory: (key: K) => T, options: GlobalAtomFamilyOptions): GlobalAtomFamily<T, K>

valdres Creates a keyed collection of cross-store singleton atoms

Combines atomFamily and globalAtom: each member is keyed by a parameter, and each member's value is shared across every store that touches it.

options.name is required — it is the family's global address. Re-defining a family under the same name returns the existing family instead of creating a second one, so calling globalAtomFamily again with the same name is safe (and idempotent) wherever it happens.

Usage

import { globalAtomFamily } from "valdres"

const featureFlagAtom = globalAtomFamily((key: string) => false, {
    name: "app/feature-flags",
})

featureFlagAtom("dark-mode").setSelf(true)

Same call shape as atomFamily — the only difference is that options.name is required instead of optional.

Each member carries the same getSelf / setSelf / resetSelf accessors as a plain globalAtom:

const flag = featureFlagAtom("dark-mode")
flag.getSelf()   // read without a Store handle
flag.setSelf(true)

Parameters

globalAtomFamily accepts the same options as atomFamily (keyOf, plus every AtomOptions field) as GlobalAtomFamilyOptions — identical to AtomFamilyOptions, except name is required instead of optional.

See also