Skip to content

Commit 8cc3d43

Browse files
committed
Fix promise result and update docs for manual option
1 parent 78408ff commit 8cc3d43

File tree

2 files changed

+77
-11
lines changed

2 files changed

+77
-11
lines changed

README.md

Lines changed: 76 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -212,29 +212,94 @@ function Todos() {
212212
})
213213
}
214214

215-
return (
215+
return isLoading ? (
216+
<span>Loading...</span>
217+
) : error ? (
218+
<span>Error: {error.message}</span>
219+
) : data ? (
216220
<>
217-
{isLoading && <span>Loading…</span>}
218-
{error && <span>Error: {error.message}</span>}
219-
{data && (
220-
<ul>
221-
{data.todos.map(todo => (
222-
<li key={todo.id}>{todo.title}</li>
223-
))}
224-
</ul>
225-
)}
221+
<ul>
222+
{data.todos.map(todo => (
223+
<li key={todo.id}>{todo.title}</li>
224+
))}
225+
</ul>
226226
{data.pagination.hasMore && (
227227
<button disabled={isFetching} onClick={onFetchMore}>
228228
{isFetching ? 'Loading more todos...' : 'Load more todos'}
229229
</button>
230230
)}
231231
</>
232-
)
232+
) : null
233233
}
234234
```
235235

236236
To prevent you from managing the loading state of `refetch` manually (since `isLoading` will remain false when `refetch` is called), React Query exposes an `isFetching` variable. It's the same as `isLoading`, but only reflects the state of the actual fetch operation for the query.
237237

238+
### Manual Querying
239+
240+
If you ever want to disable a query from automatically running when the query or variables change, you can use the `manual = true` option. When `manual` is set to true, queries will not automatically refetch due to changes to their query or variables.
241+
242+
```js
243+
function Todos() {
244+
const { data, isLoading, error, refetch, isFetching } = useQuery(
245+
fetchTodoList,
246+
{
247+
manual: true,
248+
}
249+
)
250+
251+
return (
252+
<>
253+
<button onClick={() => refetch()}>Fetch Todos</button>
254+
255+
{isLoading ? (
256+
<span>Loading...</span>
257+
) : error ? (
258+
<span>Error: {error.message}</span>
259+
) : data ? (
260+
<>
261+
<ul>
262+
{data.map(todo => (
263+
<li key={todo.id}>{todo.title}</li>
264+
))}
265+
</ul>
266+
</>
267+
) : null}
268+
</>
269+
)
270+
}
271+
```
272+
273+
This can also be useful under circumstances where you don't want your query firing because of unmet required conditions like a missing variable
274+
275+
```js
276+
function TodosByUser({ userID }) {
277+
const { data, isLoading, error, refetch, isFetching } = useQuery(
278+
fetchTodoListByUserID,
279+
{
280+
manual: !userID, // Don't auto fetch if there is no userID
281+
variables: {
282+
userID,
283+
},
284+
}
285+
)
286+
287+
return isLoading ? (
288+
<span>Loading...</span>
289+
) : error ? (
290+
<span>Error: {error.message}</span>
291+
) : data ? (
292+
<>
293+
<ul>
294+
{data.map(todo => (
295+
<li key={todo.id}>{todo.title}</li>
296+
))}
297+
</ul>
298+
</>
299+
) : null
300+
}
301+
```
302+
238303
### Retries
239304

240305
When a `useQuery` query fails (the function throws an error), React Query will automatically retry the query if that query's request has not reached the max number of consecutive retries (defaults to `3`).

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,7 @@ export function useMutation(
473473
if (userUpdateQueries) {
474474
updateQueries(userUpdateQueries, res)
475475
}
476+
return res
476477
} catch (error) {
477478
console.error(error)
478479
setError(error)

0 commit comments

Comments
 (0)