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

quick-start.md

60 lines1.4 KB

1---
2id: quick-start
3title: Quick Start
4---
5​
6Create one `QueryClient` for the browser application, provide it above query
7consumers, and call the Octane hooks from component bodies.
8​
9```tsrx
10import {
11 QueryClient,
12 QueryClientProvider,
13 useMutation,
14 useQuery,
15 useQueryClient,
16} from '@tanstack/octane-query'
17import { addTodo, getTodos } from './api'
18​
19const queryClient = new QueryClient()
20​
21function Todos() @{
22 const client = useQueryClient()
23 const todos = useQuery({
24 queryKey: ['todos'],
25 queryFn: getTodos,
26 })
27 const addTodoMutation = useMutation({
28 mutationFn: addTodo,
29 onSuccess: async () => {
30 await client.invalidateQueries({ queryKey: ['todos'] })
31 },
32 })
33​
34 @if (todos.isPending) {
35 <p>{'Loading...'}</p>
36 } @else if (todos.isError) {
37 <p>{todos.error.message}</p>
38 } @else {
39 <ul>
40 {todos.data.map((todo) => <li>{todo.title}</li>)}
41 </ul>
42 <button
43 disabled={addTodoMutation.isPending}
44 onClick={() => addTodoMutation.mutate({ title: 'Read the Query docs' })}
45 >
46 {'Add todo'}
47 </button>
48 }
50​
51function App() @{
52 <QueryClientProvider client={queryClient}>
53 <Todos />
54 </QueryClientProvider>
56```
57​
58Query option objects, keys, invalidation, retries, caching, and mutations use
59the same TanStack Query contracts as the other framework adapters. The Octane
60adapter changes the rendering and subscription layer, not Query Core.