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

Function Fetcher

packages/xstate-react/test/useActor.test.tsx:90–121  ·  view source on GitHub ↗
({
    onFetch = () => {
      return new Promise((res) => res('some data'));
    },
    persistedState
  })

Source from the content-addressed store, hash-verified

88 onFetch: () => Promise<any>;
89 persistedState?: Snapshot<unknown>;
90 }> = ({
91 onFetch = () => {
92 return new Promise((res) => res('some data'));
93 },
94 persistedState
95 }) => {
96 const [current, send] = useActor(
97 fetchMachine.provide({
98 actors: {
99 fetchData: fromPromise(onFetch)
100 }
101 }),
102 {
103 snapshot: persistedState
104 }
105 );
106
107 switch (current.value) {
108 case 'idle':
109 return <button onClick={(_) => send({ type: 'FETCH' })}>Fetch</button>;
110 case 'loading':
111 return <div>Loading...</div>;
112 case 'success':
113 return (
114 <div>
115 Success! Data: <div data-testid="data">{current.context.data}</div>
116 </div>
117 );
118 default:
119 return null;
120 }
121 };
122
123 it('should work with the useActor hook', async () => {
124 render(<Fetcher onFetch={() => new Promise((res) => res('fake data'))} />);

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…