Open History to see the last commit
1---
2id: suspensive-react-query
3title: Suspensive React Query
4---
5β
6Typesafe useQuery, useInfiniteQuery with default suspense option.
7β
8Use @suspensive/react-query, delegate loading and error handling to the outside of the component with useSuspenseQuery and useSuspenseInfiniteQuery, and focus on success inside the component.
9β
10You don't even need to use the isSuccess flag.
11β
12## Installation
13You can install @suspensive/react-query via [NPM](https://www.npmjs.com/package/@suspensive/react-query).
14β
15```bash
16$ npm i @suspensive/react @suspensive/react-query
17# or
18$ pnpm add @suspensive/react @suspensive/react-query
19# or
20$ yarn add @suspensive/react @suspensive/react-query
21```
22β
23### Motivation
24β
25If you turn suspense mode on in @tanstack/react-query, You can use useQuery with Suspense and ErrorBoundary.
26β
27```tsx
28import { useQuery } from '@tanstack/react-query'
29β
30const Example = () => {
31 const query = useQuery(queryKey, queryFn, {
32 suspense: true,
33 })
34β
35 query.data // TData | undefined
36β
37 if (query.isSuccess) {
38 query.data // TData
39 }
40}
41```
42β
43Typically query.data will be `TData | undefined` like this code example.
44But 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.
45β
46This is why @suspensive/react-query provide **useSuspenseQuery**
47β
48## useSuspenseQuery
49β
50Return type of this hook have no isLoading, isError property. because Suspense and ErrorBoundary will guarantee this hook's data.
51β
52In addition, this hook's options have default suspense: true. and you can provide new options to this hook like useQuery of @tanstack/react-query.
53β
54```tsx
55import { useSuspenseQuery } from '@suspensive/react-query'
56β
57const Example = () => {
58 const query = useSuspenseQuery(queryKey, queryFn, options) // suspense:true is default.
59β
60 // No need to do type narrowing by isSuccess
61 query.data // TData
62}
63```
64β
65### Concentrate on only Success
66β
67Now, we can concentrate component as any fetching will be always success in component.
68β
69Check the complete documentation on [GitHub](https://github.com/suspensive/react).