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

Function useActorRef

packages/xstate-react/src/useActorRef.ts:55–96  ·  view source on GitHub ↗
(
  machine: TLogic,
  ...[options, observerOrListener]: IsNotNever<
    RequiredActorOptionsKeys<TLogic>
  > extends true
    ? [
        options: ActorOptions<TLogic> & {
          [K in RequiredActorOptionsKeys<TLogic>]: unknown;
        },
        observerOrListener?:
          | Observer<SnapshotFrom<TLogic>>
          | ((value: SnapshotFrom<TLogic>) => void)
      ]
    : [
        options?: ActorOptions<TLogic>,
        observerOrListener?:
          | Observer<SnapshotFrom<TLogic>>
          | ((value: SnapshotFrom<TLogic>) => void)
      ]
)

Source from the content-addressed store, hash-verified

53}
54
55export function useActorRef<TLogic extends AnyActorLogic>(
56 machine: TLogic,
57 ...[options, observerOrListener]: IsNotNever<
58 RequiredActorOptionsKeys<TLogic>
59 > extends true
60 ? [
61 options: ActorOptions<TLogic> & {
62 [K in RequiredActorOptionsKeys<TLogic>]: unknown;
63 },
64 observerOrListener?:
65 | Observer<SnapshotFrom<TLogic>>
66 | ((value: SnapshotFrom<TLogic>) => void)
67 ]
68 : [
69 options?: ActorOptions<TLogic>,
70 observerOrListener?:
71 | Observer<SnapshotFrom<TLogic>>
72 | ((value: SnapshotFrom<TLogic>) => void)
73 ]
74): Actor<TLogic> {
75 const actorRef = useIdleActorRef(machine, options);
76
77 useEffect(() => {
78 if (!observerOrListener) {
79 return;
80 }
81 const sub = actorRef.subscribe(toObserver(observerOrListener));
82 return () => {
83 sub.unsubscribe();
84 };
85 }, [observerOrListener]);
86
87 useEffect(() => {
88 actorRef.start();
89
90 return () => {
91 stopRootWithRehydration(actorRef);
92 };
93 }, [actorRef]);
94
95 return actorRef;
96}

Callers 7

AppFunction · 0.90
TestFunction · 0.90
ComponentFunction · 0.90
AppFunction · 0.90
TestFunction · 0.90
ProviderFunction · 0.90
TodoFunction · 0.90

Calls 6

stopRootWithRehydrationFunction · 0.90
useIdleActorRefFunction · 0.85
toObserverFunction · 0.85
subscribeMethod · 0.65
unsubscribeMethod · 0.65
startMethod · 0.65

Tested by 5

AppFunction · 0.72
TestFunction · 0.72
ComponentFunction · 0.72
AppFunction · 0.72
TestFunction · 0.72