Open History to see the last commit
1import ReactDOM from 'react-dom/client'
2import {
3 QueryClient,
4 QueryClientProvider,
5 queryOptions,
6 useQueries,
7} from '@tanstack/react-query'
8import { ReactQueryDevtools } from '@tanstack/react-query-devtools'
9import { useState } from 'react'
10import { loadCharacter } from './character-loader'
11β
12const queryClient = new QueryClient({
13 defaultOptions: {
14 queries: {
15 retry: 0,
16 },
17 },
18})
19β
20const characterQueryOptions = (id: number) =>
21 queryOptions({
22 queryKey: ['character', id],
23 queryFn: () => loadCharacter(id),
24 staleTime: 60 * 1000,
25 })
26β
27function Characters({ ids }: { ids: Array<number> }) {
28 const characters = useQueries({
29 queries: ids.map((id) => characterQueryOptions(id)),
30 })
31β
32 return characters.map((character, index) => {
33 if (character.status === 'pending') {
34 return 'Loading...'
35 }
36 if (character.status === 'error') {
37 return 'Error...'
38 }
39 if (character.data === null) {
40 return 'Not found ...'
41 }
42β
43 return (
44 <div key={String(index)} style={{ display: 'flex' }}>
45 <img
46 width={100}
47 height={100}
48 alt={character.data.image}
49 src={character.data.image}
50 />
51 <div style={{ paddingLeft: 10, textAlign: 'left' }}>
52 <h5 style={{ marginTop: 0, marginBottom: 5 }}>
53 {character.data.name} (#{character.data.id})
54 </h5>
55 <p style={{ marginTop: 0, marginBottom: 2 }}>
56 Status: {character.status}
57 </p>
58 <p style={{ marginTop: 0, marginBottom: 2 }}>
59 Species: {character.data.species}
60 </p>
61 <p style={{ marginTop: 0, marginBottom: 2 }}>
62 Gender: {character.data.gender}
63 </p>
64 </div>
65 </div>
66 )
67 })
68}
69β
70function App() {
71 const [ids, setIds] = useState<Array<number>>([])
72 const [idsToLoad, setIdsToLoad] = useState<Array<number>>([])
73 const [error, setError] = useState('')
74β
75 return (
76 <div className="App">
77 <h2>Character fetcher</h2>
78 <div>
79 <label style={{ display: 'block' }}>
80 Character ids (numberic, comma seprated)
81 </label>
82 <input
83 style={{ display: 'inline-block' }}
84 placeholder="ids"
85 onChange={(event) => {
86 const text = event.target.value.replace(' ', '')
87 const ids = text.split(',').map((id) => parseInt(id, 10))
88 const allValid = ids.reduce(
89 (valid, id) => valid && !isNaN(id),
90 true,
91 )
92β
93 if (allValid) {
94 setIds(ids)
95 setError('')
96 } else {
97 setError('Invalid.. only use numbers and comma')
98 }
99 }}
100 />
101 </div>
102β
103 {error ? <h3 style={{ color: 'red' }}>{error}</h3> : null}
104β
105 <button
106 type="button"
107 onClick={() => {
108 setIdsToLoad(ids)
109 }}
110 >
111 Load characters
112 </button>
113 <div>
114 <h4 style={{ textAlign: 'left' }}>Characters:</h4>
115 <Characters ids={idsToLoad} />
116 </div>
117 </div>
118 )
119}
120β
121function Root() {
122 return (
123 <QueryClientProvider client={queryClient}>
124 <App />
125 <ReactQueryDevtools initialIsOpen />
126 </QueryClientProvider>
127 )
128}
129β
130const rootElement = document.getElementById('root')
131if (!rootElement) throw new Error('Missing #root element')
132ReactDOM.createRoot(rootElement).render(<Root />)