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

suspensive-react-query.md

83 lines3.1 KB

1---
2id: suspensive-react-query
3title: Suspensive React Query
4---
5​
6Typesafe useQuery, useQueries, useInfiniteQuery with default suspense option.
7​
8Use @suspensive/react-query, delegate loading and error handling to the outside of the component with [useSuspenseQuery](https://suspensive.org/docs/react-query/src/useSuspenseQuery.i18n), [useSuspenseQueries](https://suspensive.org/docs/react-query/src/useSuspenseQueries.i18n) and [useSuspenseInfiniteQuery](https://suspensive.org/docs/react-query/src/useSuspenseInfiniteQuery.i18n), and focus on success inside the component.
9​
10You don't even need to use the isSuccess flag.
11​
12## Installation
13​
14You can install @suspensive/react-query via [NPM](https://www.npmjs.com/package/@suspensive/react-query).
15​
16```bash
17$ npm i @suspensive/react-query
18# or
19$ pnpm add @suspensive/react-query
20# or
21$ yarn add @suspensive/react-query
22```
23​
24### Motivation
25​
26If you turn suspense mode on in @tanstack/react-query, You can use useQuery with Suspense and ErrorBoundary.
27​
28```tsx
29import { useQuery } from '@tanstack/react-query'
30​
31const Example = () => {
32 const query = useQuery({
33 queryKey,
34 queryFn,
35 suspense: true,
36 })
37​
38 query.data // TData | undefined
39​
40 if (query.isSuccess) {
41 query.data // TData
42 }
44```
45​
46Typically query.data will be `TData | undefined` like this code example.
47But actual useQuery's return type:query.data will be always fulfilled because of [Suspense](https://suspensive.org/docs/react/src/Suspense.i18n) and [ErrorBoundary](https://suspensive.org/docs/react/src/ErrorBoundary.i18n) as parent of this component.
48​
49This is why @suspensive/react-query provide **useSuspenseQuery**
50​
51## useSuspenseQuery
52​
53Return type of this hook have no isLoading, isError property. because Suspense and ErrorBoundary will guarantee this hook's data.
54​
55In addition, this hook's options have default suspense: true. and you can provide new options to this hook like useQuery of @tanstack/react-query.
56​
57```tsx
58import { useSuspenseQuery } from '@suspensive/react-query'
59​
60const Example = () => {
61 const query = useSuspenseQuery({
62 queryKey,
63 queryFn,
64 }) // suspense:true is default.
65​
66 // No need to do type narrowing by isSuccess
67 query.data // TData
69```
70​
71### Concentrate on only Success
72​
73Now, we can concentrate component as any fetching will be always success in component.
74​
75### useSuspenseQuery is Official API now! (from v5)
76​
77@suspensive/react-query provides not only [useSuspenseQuery](https://suspensive.org/docs/react-query/src/useSuspenseQuery.i18n), also [useSuspenseQueries](https://suspensive.org/docs/react-query/src/useSuspenseQueries.i18n), [useSuspenseInfiniteQuery](https://suspensive.org/docs/react-query/src/useSuspenseInfiniteQuery.i18n). From @tanstack/react-query v5 provides [official public hook apis for suspense](https://tanstack.com/query/v5/docs/react/guides/suspense) like @suspensive/react-query's hooks. If want to use them early in v4, use this @suspensive/react-query first.
78​
79Check the complete documentation on [Suspensive Official Docs Site](https://suspensive.org/) and also welcome Pull Request on [Suspensive GitHub](https://github.com/suspensive/react)
80​
81​
82​
83​