@tanstack/lit-query

中文文档
0.2.33发布于 2026/11/02MITTypeScriptESM + CJS构建来源已验证

Lit adapter for TanStack Query Core

原始使用文档
文档目录7 节
  1. Install
  2. Quick Start
  3. API Surface
  4. Runnable Examples
  5. Integration Smoke
  6. Quality Gates
  7. License
TanStack Lit Query
# @tanstack/lit-query

Lit adapter for @tanstack/query-core using Lit reactive controllers.

This package is currently experimental and v0.1. Pin exact versions if you use it in production while the API is early.

Install

npm install @tanstack/lit-query

For local development in this repository:

pnpm install
pnpm --dir packages/lit-query run build

Quick Start

import { LitElement, html } from 'lit'
import {
  QueryClient,
  QueryClientProvider,
  createQueryController,
} from '@tanstack/lit-query'

const client = new QueryClient({
  defaultOptions: { queries: { retry: false } },
})

class AppProvider extends QueryClientProvider {
  constructor() {
    super()
    this.client = client
  }
}
customElements.define('app-provider', AppProvider)

class UsersView extends LitElement {
  private readonly users = createQueryController(this, {
    queryKey: ['users'],
    queryFn: async () => {
      const response = await fetch('/api/users')
      return response.json() as Promise<Array<{ id: string; name: string }>>
    },
  })

  render() {
    const query = this.users()
    if (query.isPending) return html`Loading...`
    if (query.isError) return html`Error`
    return html`<ul>
      ${query.data?.map((u) => html`<li>${u.name}</li>`)}
    </ul>`
  }
}
customElements.define('users-view', UsersView)

API Surface

  • QueryClientProvider, useQueryClient, resolveQueryClient
  • createQueryController
  • createMutationController
  • createInfiniteQueryController
  • createQueriesController
  • useIsFetching, useIsMutating, useMutationState
  • queryOptions, infiniteQueryOptions, mutationOptions

Runnable Examples

This repo includes runnable Lit examples under the top-level examples/lit directory so they can be surfaced in the docs:

  • examples/lit/basic: Vite Lit app covering query and mutation primitives.
  • examples/lit/pagination: pagination, prefetching, optimistic updates, and error recovery.
  • examples/lit/ssr: Lit SSR render, dehydrate, and hydrate flow.

Run an example from the repo root:

pnpm --dir examples/lit/basic run dev
pnpm --dir examples/lit/pagination run dev
pnpm --dir examples/lit/ssr run dev

Open:

  • http://127.0.0.1:4173/ (basic example)
  • http://127.0.0.1:4183/ (pagination example app)
  • http://127.0.0.1:4174/ (SSR example app)

Use a different port (optional):

DEMO_PORT=4180 pnpm --dir examples/lit/basic run dev
PAGINATION_DEMO_PORT=4181 PAGINATION_API_PORT=4182 pnpm --dir examples/lit/pagination run dev
SSR_PORT=4180 pnpm --dir examples/lit/ssr run dev
SSR_HOST=0.0.0.0 pnpm --dir examples/lit/ssr run dev

Integration Smoke

For the framework build smoke used in CI:

pnpm --dir integrations/lit/vite run build

Quality Gates

From the repository root:

pnpm --dir packages/lit-query run test:types
pnpm --dir packages/lit-query run test:lib
pnpm --dir packages/lit-query run build
pnpm --dir packages/lit-query run test:build

License

MIT

版本与发布记录

@tanstack/lit-query 共发布 35 个版本,首次发布于 2026/05/06;过去 12 个月发布了 35 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.2.33latest2026/11/02今天
0.2.322026/11/012 天前
0.2.312026/10/268 天前
0.2.302026/10/2014 天前
0.2.292026/10/1816 天前
0.2.282026/10/1519 天前

关于 @tanstack/lit-query 的常见问题

@tanstack/lit-query 是什么?

点亮适配器为TanStack查询核心

如何安装 @tanstack/lit-query?

在项目目录运行 npm install @tanstack/lit-query,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:lit(>=2.8.0 <4)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@tanstack/lit-query 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@tanstack/lit-query 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@tanstack/lit-query 可以商用吗?

@tanstack/lit-query 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@tanstack/lit-query 还在维护吗?

最新版本 0.2.33 发布于 2026/11/02(今天);至今共发布 35 个版本,其中过去 12 个月发布了 35 个;首次发布于 2026/05/06。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。