MCPcopy
hub / github.com/statelyai/xstate / useAtom

Function useAtom

packages/xstate-store-react/src/index.ts:238–256  ·  view source on GitHub ↗
(
  definition: AnyAtom | AtomConfig<any, any>,
  selectorOrInput?: any,
  compare = defaultCompare
)

Source from the content-addressed store, hash-verified

236 compare?: (a: S, b: S) => boolean
237): S;
238export function useAtom(
239 definition: AnyAtom | AtomConfig<any, any>,
240 selectorOrInput?: any,
241 compare = defaultCompare
242) {
243 const atomRef = useRef<any>(undefined);
244
245 if (isAtom(definition)) {
246 return useSelector(definition, selectorOrInput ?? identity, compare);
247 }
248
249 if (!atomRef.current) {
250 atomRef.current = definition.createAtom(selectorOrInput);
251 }
252
253 const state = useSelector(atomRef.current, identity, compare);
254
255 return state;
256}
257
258/**
259 * Creates or subscribes to an atom for the lifetime of a React component.

Callers 3

TestComponentFunction · 0.90
ComponentFunction · 0.90
useAtomStateFunction · 0.70

Calls 2

isAtomFunction · 0.70
useSelectorFunction · 0.70

Tested by 2

TestComponentFunction · 0.72
ComponentFunction · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…