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

suspense.md

42 lines1.5 KB

1---
2id: suspense
3title: Suspense
4---
5​
6Solid Query can also be used with Solid's [Suspense](https://docs.solidjs.com/reference/components/suspense) API's.
7​
8To do that you need to wrap your suspendable component with `Suspense` component provided by Solid
9​
10```tsx
11import { Suspense } from 'solid-js'
12;<Suspense fallback={<LoadingSpinner />}>
13 <SuspendableComponent />
14</Suspense>
15```
16​
17You can use async `suspense` function that is provided by `solid-query`.
18​
19```vue
20<script>
21import { defineComponent } from 'vue'
22import { useQuery } from '@tanstack/solid-query'
23​
24const todoFetcher = async () =>
25 await fetch('https://jsonplaceholder.cypress.io/todos').then((response) =>
26 response.json(),
27 )
28export default defineComponent({
29 name: 'SuspendableComponent',
30 async setup() {
31 const { data, suspense } = useQuery(() => ['todos'], todoFetcher)
32 await suspense()
33​
34 return { data }
35 },
36})
37</script>
38```
39​
40## Fetch-on-render vs Render-as-you-fetch
41​
42Out of the box, Solid Query in `suspense` mode works really well as a **Fetch-on-render** solution with no additional configuration. This means that when your components attempt to mount, they will trigger query fetching and suspend, but only once you have imported them and mounted them. If you want to take it to the next level and implement a **Render-as-you-fetch** model, we recommend implementing [Prefetching](../prefetching) on routing callbacks and/or user interactions events to start loading queries before they are mounted and hopefully even before you start importing or mounting their parent components.