/* @refresh reload */ import { QueryClient, QueryClientProvider, useQuery, } from '@tanstack/solid-query' import { SolidQueryDevtools } from '@tanstack/solid-query-devtools' import { For, Match, Switch, createSignal } from 'solid-js' import { render } from 'solid-js/web' import type { Component, Setter } from 'solid-js' const queryClient = new QueryClient({ defaultOptions: { queries: { gcTime: 1000 * 60 * 60 * 24, // 24 hours }, }, }) type Post = { id: number title: string body: string } function createPosts() { return useQuery(() => ({ queryKey: ['posts'], queryFn: async (): Promise> => { const response = await fetch('https://jsonplaceholder.typicode.com/posts') return await response.json() }, })) } function Posts(props: { setPostId: Setter }) { const state = createPosts() return (

Posts

Loading... Error: {(state.error as Error).message} <>
{state.isFetching ? 'Background Updating...' : ' '}
) } const getPostById = async (id: number): Promise => { const response = await fetch( `https://jsonplaceholder.typicode.com/posts/${id}`, ) return await response.json() } function createPost(postId: number) { return useQuery(() => ({ queryKey: ['post', postId], queryFn: () => getPostById(postId), enabled: !!postId, })) } function Post(props: { postId: number; setPostId: Setter }) { const state = createPost(props.postId) return (
Loading... Error: {(state.error as Error).message} <>

{state.data?.title}

{state.data?.body}

{state.isFetching ? 'Background Updating...' : ' '}
) } const App: Component = () => { const [postId, setPostId] = createSignal(-1) return (

As you visit the posts below, you will notice them in a loading state the first time you load them. However, after you return to this list and click on any posts you have already visited again, you will see them load instantly and background refresh right before your eyes!{' '} (You may need to throttle your network speed to simulate longer loading sequences)

{postId() > -1 ? ( ) : ( )}
) } render(() => , document.getElementById('root') as HTMLElement)