Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

app.tsrx

56 lines1.6 KB

1import {
2 QueryClientProvider,
3 useQuery,
4 useMutation,
5 useQueryClient,
6} from '@tanstack/octane-query';
7​
8// Reads a query through the CONTEXT client (no explicit client arg).
9export function Reader(props: { queryFn: () => Promise<string> }) @{
10 const q = useQuery({ queryKey: ['k'], queryFn: props.queryFn, retry: false });
11 <div id="status">{(q.isPending
12 ? 'pending'
13 : q.isError
14 ? 'error:' + (q.error as Error).message
15 : 'data:' + q.data) as string}</div>
17​
18export function App(props: { client: unknown; queryFn: () => Promise<string> }) @{
19 <QueryClientProvider client={props.client}>
20 <Reader queryFn={props.queryFn} />
21 </QueryClientProvider>
23​
24// useQueryClient resolves the provided client.
25export function ClientProbe(props: { onClient: (c: unknown) => void }) @{
26 const client = useQueryClient();
27 props.onClient(client);
28 <span id="probe">ok</span>
30​
31export function ProbeApp(props: { client: unknown; onClient: (c: unknown) => void }) @{
32 <QueryClientProvider client={props.client}>
33 <ClientProbe onClient={props.onClient} />
34 </QueryClientProvider>
36​
37// Mutation lifecycle.
38export function Mutator(props: { mutationFn: (v: string) => Promise<string> }) @{
39 const m = useMutation({ mutationFn: props.mutationFn });
40 <div>
41 <span id="mstatus">{(m.isIdle
42 ? 'idle'
43 : m.isPending
44 ? 'pending'
45 : m.isError
46 ? 'error'
47 : 'data:' + m.data) as string}</span>
48 <button id="go" onClick={() => m.mutate('x')}>go</button>
49 </div>
51​
52export function MutApp(props: { client: unknown; mutationFn: (v: string) => Promise<string> }) @{
53 <QueryClientProvider client={props.client}>
54 <Mutator mutationFn={props.mutationFn} />
55 </QueryClientProvider>