Skip to content

Commit e152ea5

Browse files
authored
docs: prefer object syntax in docs (#4409)
* docs: useQuery to object syntax * docs: invalidateQueries to object syntax * docs: useMutation to object syntax * docs: infinite query and other references to object syntax
1 parent 6178b56 commit e152ea5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+349
-302
lines changed

docs/adapters/react-query.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { QueryClient, QueryClientProvider, useQuery } from '@tanstack/react-quer
1212
const queryClient = new QueryClient()
1313

1414
function Example() {
15-
const query = useQuery(['todos'], fetchTodos)
15+
const query = useQuery({ queryKey: ['todos'], queryFn: fetchTodos })
1616

1717
return (
1818
<div>

docs/adapters/vue-query.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@ import { useQueryClient, useQuery, useMutation } from "@tanstack/vue-query";
2020
const queryClient = useQueryClient();
2121
2222
// Query
23-
const { isLoading, isError, data, error } = useQuery(["todos"], getTodos);
23+
const { isLoading, isError, data, error } = useQuery({ queryKey: ['todos'], queryFn: getTodos });
2424
2525
// Mutation
26-
const mutation = useMutation(postTodo, {
26+
const mutation = useMutation({
27+
mutationFn: postTodo,
2728
onSuccess: () => {
2829
// Invalidate and refetch
29-
queryClient.invalidateQueries(["todos"]);
30+
queryClient.invalidateQueries({ queryKey: ["todos"] });
3031
},
3132
});
3233

docs/community/lukemorales-query-key-factory.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,13 @@ import { queryKeys, completeTodo, fetchSingleTodo } from '../my-api'
6060
export function Todo({ todoId }) {
6161
const queryClient = useQueryClient()
6262

63-
const query = useQuery(queryKeys.todos.byId(todoId), fetchSingleTodo)
63+
const query = useQuery({ queryKey: queryKeys.todos.byId(todoId), queryFn: fetchSingleTodo })
6464

65-
const mutation = useMutation(completeTodo, {
65+
const mutation = useMutation({
66+
mutationFn: () => completeTodo,
6667
onSuccess: () => {
6768
// Invalidate and refetch
68-
queryClient.invalidateQueries(queryKeys.todos.completed)
69+
queryClient.invalidateQueries({ queryKey: queryKeys.todos.completed })
6970
},
7071
})
7172

docs/comparison.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ Feature/Capability Key:
6969
7070
> **<sup>3</sup> Partial query matching** - Because React Query uses deterministic query key serialization, this allows you to manipulate variable groups of queries without having to know each individual query-key that you want to match, eg. you can refetch every query that starts with `todos` in its key, regardless of variables, or you can target specific queries with (or without) variables or nested properties, and even use a filter function to only match queries that pass your specific conditions.
7171
72-
> **<sup>4</sup> Pre-usage Query Configuration** - This is simply a fancy name for being able to configure how queries and mutations will behave before they are used. For instance, a query can be fully configured with defaults beforehand and when the time comes to use it, only `useQuery(key)` is necessary, instead of being required to pass the fetcher and/or options with every usage. SWR does have a partial form of this feature by allowing you to pre-configure a default fetcher, but only as a global fetcher, not on a per-query basis and definitely not for mutations.
72+
> **<sup>4</sup> Pre-usage Query Configuration** - This is simply a fancy name for being able to configure how queries and mutations will behave before they are used. For instance, a query can be fully configured with defaults beforehand and when the time comes to use it, only `useQuery({ queryKey })` is necessary, instead of being required to pass the fetcher and/or options with every usage. SWR does have a partial form of this feature by allowing you to pre-configure a default fetcher, but only as a global fetcher, not on a per-query basis and definitely not for mutations.
7373
7474
> **<sup>5</sup> Automatic Refetch after Mutation** - For truly automatic refetching to happen after a mutation occurs, a schema is necessary (like the one graphQL provides) along with heuristics that help the library know how to identify individual entities and entities types in that schema.
7575

docs/graphql.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,16 @@ const allFilmsWithVariablesQueryDocument = graphql(/* GraphQL */ `
3333

3434
function App() {
3535
// `data` is fully typed!
36-
const { data } = useQuery(['films'], async () =>
37-
request(
38-
'https://swapi-graphql.netlify.app/.netlify/functions/index',
39-
allFilmsWithVariablesQueryDocument,
40-
// variables are type-checked too!
41-
{ first: 10 }
42-
)
43-
);
44-
36+
const { data } = useQuery({
37+
queryKey: ['films'],
38+
queryFn: async () =>
39+
request(
40+
'https://swapi-graphql.netlify.app/.netlify/functions/index',
41+
allFilmsWithVariablesQueryDocument,
42+
// variables are type-checked too!
43+
{first: 10}
44+
),
45+
})
4546
// ...
4647
}
4748
```

docs/guides/background-fetching-indicators.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ A query's `status === 'loading'` state is sufficient enough to show the initial
77

88
```tsx
99
function Todos() {
10-
const { status, data: todos, error, isFetching } = useQuery(
11-
['todos'],
12-
fetchTodos
13-
)
10+
const { status, data: todos, error, isFetching } = useQuery({
11+
queryKey: ['todos'],
12+
queryFn: fetchTodos,
13+
})
1414

1515
return status === 'loading' ? (
1616
<span>Loading...</span>

docs/guides/caching.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,18 @@ This caching example illustrates the story and lifecycle of:
1616

1717
Let's assume we are using the default `cacheTime` of **5 minutes** and the default `staleTime` of `0`.
1818

19-
- A new instance of `useQuery(['todos'], fetchTodos)` mounts.
19+
- A new instance of `useQuery({ queryKey: ['todos'], queryFn: fetchTodos })` mounts.
2020
- Since no other queries have been made with the `['todos']` query key, this query will show a hard loading state and make a network request to fetch the data.
2121
- When the network request has completed, the returned data will be cached under the `['todos']` key.
2222
- The hook will mark the data as stale after the configured `staleTime` (defaults to `0`, or immediately).
23-
- A second instance of `useQuery(['todos'], fetchTodos)` mounts elsewhere.
23+
- A second instance of `useQuery({ queyKey: ['todos'], queryFn: fetchTodos })` mounts elsewhere.
2424
- Since the cache already has data for the `['todos']` key from the first query, that data is immediately returned from the cache.
2525
- The new instance triggers a new network request using its query function.
2626
- Note that regardless of whether both `fetchTodos` query functions are identical or not, both queries' [`status`](../reference/useQuery) are updated (including `isFetching`, `isLoading`, and other related values) because they have the same query key.
2727
- When the request completes successfully, the cache's data under the `['todos']` key is updated with the new data, and both instances are updated with the new data.
28-
- Both instances of the `useQuery(['todos'], fetchTodos)` query are unmounted and no longer in use.
28+
- Both instances of the `useQuery({ queryKey: ['todos'], queryFn: fetchTodos })` query are unmounted and no longer in use.
2929
- Since there are no more active instances of this query, a cache timeout is set using `cacheTime` to delete and garbage collect the query (defaults to **5 minutes**).
30-
- Before the cache timeout has completed, another instance of `useQuery(['todos'], fetchTodos)` mounts. The query immediately returns the available cached data while the `fetchTodos` function is being run in the background. When it completes successfully, it will populate the cache with fresh data.
31-
- The final instance of `useQuery(['todos'], fetchTodos)` unmounts.
32-
- No more instances of `useQuery(['todos'], fetchTodos)` appear within **5 minutes**.
30+
- Before the cache timeout has completed, another instance of `useQuery({ queryKey: ['todos'], queyFn: fetchTodos })` mounts. The query immediately returns the available cached data while the `fetchTodos` function is being run in the background. When it completes successfully, it will populate the cache with fresh data.
31+
- The final instance of `useQuery({ queryKey: ['todos'], queryFn: fetchTodos })` unmounts.
32+
- No more instances of `useQuery({ queyKey: ['todos'], queryFn: fetchTodos })` appear within **5 minutes**.
3333
- The cached data under the `['todos']` key is deleted and garbage collected.

docs/guides/default-query-function.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,15 @@ function App() {
3131

3232
// All you have to do now is pass a key!
3333
function Posts() {
34-
const { status, data, error, isFetching } = useQuery(['/posts'])
34+
const { status, data, error, isFetching } = useQuery({ queryKey: ['/posts'] })
3535

3636
// ...
3737
}
3838

3939
// You can even leave out the queryFn and just go straight into options
4040
function Post({ postId }) {
41-
const { status, data, error, isFetching } = useQuery([`/posts/${postId}`], {
41+
const { status, data, error, isFetching } = useQuery({
42+
queryKey: [`/posts/${postId}`],
4243
enabled: !!postId,
4344
})
4445

docs/guides/dependent-queries.md

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,16 @@ Dependent (or serial) queries depend on previous ones to finish before they can
77

88
```tsx
99
// Get the user
10-
const { data: user } = useQuery(['user', email], getUserByEmail)
10+
const { data: user } = useQuery({ queryKey: ['user', email], queryFn: getUserByEmail })
1111

1212
const userId = user?.id
1313

1414
// Then get the user's projects
15-
const { status, fetchStatus, data: projects } = useQuery(
16-
['projects', userId],
17-
getProjectsByUser,
18-
{
19-
// The query will not execute until the userId exists
20-
enabled: !!userId,
21-
}
15+
const { status, fetchStatus, data: projects } = useQuery({
16+
queryKey: ['projects', userId],
17+
queryFn: getProjectsByUser,
18+
// The query will not execute until the userId exists
19+
enabled: !!userId,
2220
)
2321
```
2422

docs/guides/disabling-queries.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ function Todos() {
2525
error,
2626
refetch,
2727
isFetching
28-
} = useQuery(['todos'], fetchTodoList, {
28+
} = useQuery({
29+
queryKey: ['todos'],
30+
queyFn: fetchTodoList,
2931
enabled: false,
3032
})
3133

@@ -69,14 +71,12 @@ The enabled option can not only be used to permanently disable a query, but also
6971
function Todos() {
7072
const [filter, setFilter] = React.useState('')
7173

72-
const { data } = useQuery(
73-
['todos', filter],
74-
() => fetchTodos(filter),
75-
{
74+
const { data } = useQuery({
75+
queryKey: ['todos', filter],
76+
queryFn: () => fetchTodos(filter),
7677
// ⬇️ disabled as long as the filter is empty
7778
enabled: !!filter
78-
}
79-
)
79+
})
8080

8181
return (
8282
<div>

0 commit comments

Comments
 (0)