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

Function useIdleActorRef

packages/xstate-react/src/useActorRef.ts:18–53  ·  view source on GitHub ↗
(
  logic: TLogic,
  ...[options]: ConditionalRequired<
    [
      options?: ActorOptions<TLogic> & {
        [K in RequiredActorOptionsKeys<TLogic>]: unknown;
      }
    ],
    IsNotNever<RequiredActorOptionsKeys<TLogic>>
  >
)

Source from the content-addressed store, hash-verified

16import { stopRootWithRehydration } from './stopRootWithRehydration';
17
18export function useIdleActorRef<TLogic extends AnyActorLogic>(
19 logic: TLogic,
20 ...[options]: ConditionalRequired<
21 [
22 options?: ActorOptions<TLogic> & {
23 [K in RequiredActorOptionsKeys<TLogic>]: unknown;
24 }
25 ],
26 IsNotNever<RequiredActorOptionsKeys<TLogic>>
27 >
28): Actor<TLogic> {
29 let [[currentConfig, actorRef], setCurrent] = useState(() => {
30 const actorRef = createActor(logic, options);
31 return [logic.config, actorRef];
32 });
33
34 if (logic.config !== currentConfig) {
35 const newActorRef = createActor(logic, {
36 ...options,
37 snapshot: (actorRef.getPersistedSnapshot as any)({
38 __unsafeAllowInlineActors: true
39 })
40 });
41 setCurrent([logic.config, newActorRef]);
42 actorRef = newActorRef;
43 }
44
45 // TODO: consider using `useAsapEffect` that would do this in `useInsertionEffect` is that's available
46 useIsomorphicLayoutEffect(() => {
47 (actorRef.logic as any as AnyStateMachine).implementations = (
48 logic as any as AnyStateMachine
49 ).implementations;
50 });
51
52 return actorRef;
53}
54
55export function useActorRef<TLogic extends AnyActorLogic>(
56 machine: TLogic,

Callers 2

useActorFunction · 0.90
useActorRefFunction · 0.85

Calls 1

createActorFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…