Solid Query 是 TanStack Query 的官方 SolidJS 适配器,它可以轻松地在您的 Web 应用程序中实现获取、缓存、同步和更新服务器状态。
SolidJS 作为一种快速、响应式和声明式的前端库,在构建用户界面方面越来越受欢迎。它开箱即用地提供了许多功能。像 createSignal、createStore 这样的原语非常适合管理客户端状态。而且,与其他 UI 库不同,SolidJS 在管理异步数据方面有其独特的理念。createResource API 是处理 SolidJS 应用中服务器状态的绝佳原语。一个 resource 是一种特殊的信号,可以在数据处于加载状态时触发 Suspense 边界。
import { createResource, ErrorBoundary, Suspense } from 'solid-js'
import { render } from 'solid-js/web'
function App() {
const [repository] = createResource(async () => {
const result = await fetch('https://api.github.com/repos/TanStack/query')
if (!result.ok) throw new Error('Failed to fetch data')
return result.json()
})
return (
<div>
<div>Static Content</div>
{/* An error while fetching will be caught by the ErrorBoundary */}
<ErrorBoundary fallback={<div>Something went wrong!</div>}>
{/* Suspense will trigger a loading state while the data is being fetched */}
<Suspense fallback={<div>Loading...</div>}>
<div>{repository()?.updated_at}</div>
</Suspense>
</ErrorBoundary>
</div>
)
}
const root = document.getElementById('root')
render(() => <App />, root!)
import { createResource, ErrorBoundary, Suspense } from 'solid-js'
import { render } from 'solid-js/web'
function App() {
const [repository] = createResource(async () => {
const result = await fetch('https://api.github.com/repos/TanStack/query')
if (!result.ok) throw new Error('Failed to fetch data')
return result.json()
})
return (
<div>
<div>Static Content</div>
{/* An error while fetching will be caught by the ErrorBoundary */}
<ErrorBoundary fallback={<div>Something went wrong!</div>}>
{/* Suspense will trigger a loading state while the data is being fetched */}
<Suspense fallback={<div>Loading...</div>}>
<div>{repository()?.updated_at}</div>
</Suspense>
</ErrorBoundary>
</div>
)
}
const root = document.getElementById('root')
render(() => <App />, root!)
这太棒了!只需几行代码,您就可以从 API 获取数据并处理加载和错误状态。但是,随着应用程序复杂度的增加,您将需要更多功能来有效地管理服务器状态。这是因为服务器状态与客户端状态完全不同。首先,服务器状态
一旦您掌握了应用程序中服务器状态的本质,**甚至会出现更多挑战**,例如
这就是Solid Query发挥作用的地方。该库封装了 createResource,并提供了一组钩子和实用工具来有效地管理服务器状态。它开箱即用,无需配置,并且可以根据您的喜好进行自定义,以适应您不断增长的应用程序。
从更技术的角度来看,Solid Query 可能会
在下面的示例中,您可以看到 Solid Query 以其最基本和最简单的形式被用来获取 TanStack Query GitHub 项目本身的 GitHub 统计信息
import { ErrorBoundary, Suspense } from 'solid-js'
import {
useQuery,
QueryClient,
QueryClientProvider,
} from '@tanstack/solid-query'
function App() {
const repositoryQuery = useQuery(() => ({
queryKey: ['TanStack Query'],
queryFn: async () => {
const result = await fetch('https://api.github.com/repos/TanStack/query')
if (!result.ok) throw new Error('Failed to fetch data')
return result.json()
},
staleTime: 1000 * 60 * 5, // 5 minutes
throwOnError: true, // Throw an error if the query fails
}))
return (
<div>
<div>Static Content</div>
{/* An error while fetching will be caught by the ErrorBoundary */}
<ErrorBoundary fallback={<div>Something went wrong!</div>}>
{/* Suspense will trigger a loading state while the data is being fetched */}
<Suspense fallback={<div>Loading...</div>}>
{/*
The `data` property on a query is a SolidJS resource
so it will work with Suspense and transitions out of the box!
*/}
<div>{repositoryQuery.data?.updated_at}</div>
</Suspense>
</ErrorBoundary>
</div>
)
}
const root = document.getElementById('root')
const client = new QueryClient()
render(
() => (
<QueryClientProvider client={client}>
<App />
</QueryClientProvider>
),
root!,
)
import { ErrorBoundary, Suspense } from 'solid-js'
import {
useQuery,
QueryClient,
QueryClientProvider,
} from '@tanstack/solid-query'
function App() {
const repositoryQuery = useQuery(() => ({
queryKey: ['TanStack Query'],
queryFn: async () => {
const result = await fetch('https://api.github.com/repos/TanStack/query')
if (!result.ok) throw new Error('Failed to fetch data')
return result.json()
},
staleTime: 1000 * 60 * 5, // 5 minutes
throwOnError: true, // Throw an error if the query fails
}))
return (
<div>
<div>Static Content</div>
{/* An error while fetching will be caught by the ErrorBoundary */}
<ErrorBoundary fallback={<div>Something went wrong!</div>}>
{/* Suspense will trigger a loading state while the data is being fetched */}
<Suspense fallback={<div>Loading...</div>}>
{/*
The `data` property on a query is a SolidJS resource
so it will work with Suspense and transitions out of the box!
*/}
<div>{repositoryQuery.data?.updated_at}</div>
</Suspense>
</ErrorBoundary>
</div>
)
}
const root = document.getElementById('root')
const client = new QueryClient()
render(
() => (
<QueryClientProvider client={client}>
<App />
</QueryClientProvider>
),
root!,
)
是的!但是,这几行代码开启了全新的可能性。在上面的示例中,您的查询将缓存 5 分钟,这意味着如果在 5 分钟内,应用程序中的任何组件挂载并使用相同的查询,它将不会重新获取数据,而是使用缓存的数据。这仅仅是 Solid Query 开箱即用提供的众多功能之一。其他一些功能包括