-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.tsx
More file actions
53 lines (43 loc) · 1.27 KB
/
main.tsx
File metadata and controls
53 lines (43 loc) · 1.27 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
52
53
import ReactDOM from "react-dom/client";
import { createApi, createSchema, createStore, mdw, timer } from "starfx";
import { Provider, useCache } from "starfx/react";
const [schema, initialState] = createSchema();
const store = createStore({ initialState });
const api = createApi();
// mdw = middleware
api.use(mdw.api({ schema }));
api.use(api.routes());
api.use(mdw.fetch({ baseUrl: "https://api.github.com" }));
const fetchRepo = api.get(
"/repos/neurosnap/starfx",
{ supervisor: timer() },
api.cache(),
);
store.initialize(api.register);
function App() {
return (
<Provider schema={schema} store={store}>
<Example />
</Provider>
);
}
function Example() {
const { isLoading, isError, message, data } = useCache(fetchRepo());
if (isLoading || !data) return "Loading ...";
if (isError) return `An error has occurred: ${message}`;
return (
<div>
<h1>{data.name}</h1>
<p>{data.description}</p>
<strong>👀 {data.subscribers_count}</strong>{" "}
<strong>✨ {data.stargazers_count}</strong>{" "}
<strong>🍴 {data.forks_count}</strong>
</div>
);
}
const root = document.getElementById("root") as HTMLElement;
ReactDOM.createRoot(root).render(
<Provider schema={schema} store={store}>
<App />
</Provider>,
);