-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathindex.tsx
executable file
·102 lines (92 loc) · 2.27 KB
/
index.tsx
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import {
QueryClient,
QueryClientProvider,
useQuery,
} from '@tanstack/react-query'
import { Suspense, use, useState, useTransition } from 'react'
import ReactDOM from 'react-dom/client'
const Example1 = ({ value }: { value: number }) => {
const { isFetching, promise } = useQuery({
queryKey: ['1' + value],
queryFn: async () => {
await new Promise((resolve) => setTimeout(resolve, 1000))
return '1' + value
},
})
const data = use(promise)
return (
<div>
{data} {isFetching ? 'fetching' : null}
</div>
)
}
const Example2 = ({ value }: { value: number }) => {
const { promise, isFetching } = useQuery({
queryKey: ['2' + value],
queryFn: async () => {
await new Promise((resolve) => setTimeout(resolve, 1000))
return '2' + value
},
// placeholderData: keepPreviousData,
})
const data = use(promise)
return (
<div>
{data} {isFetching ? 'fetching' : null}
</div>
)
}
const SuspenseBoundary = () => {
const [state, setState] = useState(-1)
const [isPending, startTransition] = useTransition()
return (
<div>
<h1>Change state with transition</h1>
<div>
<button
onClick={() =>
startTransition(() => {
setState((s) => s - 1)
})
}
>
Decrease
</button>
</div>
<h2>State:</h2>
<ul>
<li>last state value: {state}</li>
<li>
transition state: {isPending ? <strong>pending</strong> : 'idle'}
</li>
</ul>
<h2>2. 1 Suspense + startTransition</h2>
<Suspense fallback="fallback 1">
<Example1 value={state}></Example1>
</Suspense>
<h2>2.2 Suspense + startTransition</h2>
<Suspense fallback="fallback 2">
<Example2 value={state}></Example2>
</Suspense>
</div>
)
}
const queryClient = new QueryClient({
defaultOptions: {
queries: {
experimental_prefetchInRender: true,
staleTime: 10 * 1000,
},
},
})
const App = () => {
return (
<div>
<QueryClientProvider client={queryClient}>
<SuspenseBoundary />
</QueryClientProvider>
</div>
)
}
const rootElement = document.getElementById('root') as HTMLElement
ReactDOM.createRoot(rootElement).render(<App />)