Open History to see the last commit
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 }
43}
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
68}
69```
70β
71### Concentrate on only Success
72β
73Now, we can concentrate component as any fetching will be always success in component.
74β
75### More Information
76β
77Check the complete documentation on [Suspensive Official Docs Site](https://suspensive.org/) and also welcome Pull Request on [Suspensive GitHub](https://github.com/suspensive/react)