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

Function Fetcher

packages/xstate-solid/test/useActor.test.tsx:93–127  ·  view source on GitHub ↗
(props: {
    onFetch: () => Promise<any>;
    persistedState?: typeof persistedFetchState;
  })

Source from the content-addressed store, hash-verified

91 const persistedFetchState = actorRef.getPersistedSnapshot();
92
93 const Fetcher = (props: {
94 onFetch: () => Promise<any>;
95 persistedState?: typeof persistedFetchState;
96 }) => {
97 const mergedProps = mergeProps(
98 {
99 onFetch: () => new Promise((res) => res('some data'))
100 },
101 props
102 );
103 const [snapshot, send] = useActor(
104 fetchMachine.provide({
105 actors: {
106 fetchData: fromPromise(mergedProps.onFetch)
107 }
108 }),
109 {
110 snapshot: mergedProps.persistedState
111 }
112 );
113
114 return (
115 <Switch fallback={null}>
116 <Match when={snapshot.matches('idle')}>
117 <button onclick={(_) => send({ type: 'FETCH' })}>Fetch</button>;
118 </Match>
119 <Match when={snapshot.matches('loading')}>
120 <div>Loading...</div>
121 </Match>
122 <Match when={snapshot.matches('success')}>
123 Success! Data: <div data-testid="data">{snapshot.context.data}</div>
124 </Match>
125 </Switch>
126 );
127 };
128
129 it('should work (default initial snapshot)', async () => {
130 render(() => (

Callers

nothing calls this directly

Calls 4

useActorFunction · 0.90
fromPromiseFunction · 0.90
provideMethod · 0.80
sendFunction · 0.50

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…