forked from isaacplmann/sturdy-uis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.tsx
More file actions
51 lines (47 loc) · 1.26 KB
/
Copy pathApp.tsx
File metadata and controls
51 lines (47 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { useMachine } from '@xstate/react';
import React from 'react';
import { fetchPeople } from './api';
import './App.css';
import { fetchMachine } from './machines/fetch';
export interface Person {
name: string;
homeworld: string;
}
function App() {
const [fetchState, sendToFetchMachine] = useMachine(fetchMachine, {
actions: {
fetchData: (ctx, event) => {
fetchPeople()
.then(r => r.results)
.then(
res => {
sendToFetchMachine({ type: 'RESOLVE', results: res });
},
message => {
sendToFetchMachine({ type: 'REJECT', message });
}
);
}
}
});
return (
<div className="App">
<button onClick={() => sendToFetchMachine({ type: 'FETCH' })}>
Fetch
</button>
{fetchState.matches('pending') ? <p>Loading</p> : null}
{fetchState.matches('successful') ? (
<ul>
{fetchState.context.results &&
fetchState.context.results.map((person, index) => (
<li key={index}>{person.name}</li>
))}
</ul>
) : null}
{fetchState.matches('failed') ? (
<p>{fetchState.context.message}</p>
) : null}
</div>
);
}
export default App;