0
Fork 0
mirror of https://github.com/TanStack/query.git synced 2026-11-16 22:50:20 +00:00
query/docs/framework/lit/quick-start.md
Gaurav 4082894509
feat: lit query adapter
* feat lit-query: add initial lit-query package

* remove internal docs from initial PR

* fix review issues

* align scripts with monorepo Nx targets and use workspace:* for query-core

* feat(lit-query): fix type inference and monorepo integration

  - Fix createQueriesController tuple type inference with recursive types
  - Add DataTag support to queryOptions
  - Fix build config and vitest custom-condition resolution
  - Fix example install scripts for standalone bootstrap

* fix(lit-query): polish package validation and examples

* ci: apply automated fixes

* lit-query: preserve provider handshake ordering in BaseController

* Update .changeset/lemon-memes-divide.md

Co-authored-by: Dominik Dorfmeister 🔮 <office@dorfmeister.cc>

* removing prettier config from adapter, so that global config from the monorepo is used

* removed the package local format scripts so formatting stays centralized at the monorepo root

* removing like other adapters keep build and test:build as separate targets

* Compared against the other adapters and removed the lit-query-only aliases/gates, keeping the standard Nx target names so root CI can pick them up consistently

* package:check has been removed. The CJS smoke check is still needed because lit-query publishes custom CJS output/types

* fix(lit-query): align lit examples and integration with monorepo conventions

* chore(lit-query): clean up stale package checks

* fix(lit-query): address review feedback for lit examples and lifecycle tests

* chore: fix lockfile

* fix: add customCondition

* docs(lit-query): add Lit docs and generated reference

* docs(lit-query): fix Lit docs review comment and align examples

---------

Co-authored-by: gauravchugh <gauravchugh@servicenow.com>
Co-authored-by: Dominik Dorfmeister 🔮 <office@dorfmeister.cc>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
2026-05-08 15:18:50 +02:00

2.1 KiB

id title
quick-start Quick Start

This snippet shows the three core Lit Query concepts:

For complete runnable examples, see Basic, Pagination, and SSR.

import { LitElement, html } from 'lit'
import {
  QueryClient,
  QueryClientProvider,
  createMutationController,
  createQueryController,
} from '@tanstack/lit-query'
import { addTodo, getTodos } from './api'

const queryClient = new QueryClient()

class AppQueryProvider extends QueryClientProvider {
  constructor() {
    super()
    this.client = queryClient
  }
}

customElements.define('app-query-provider', AppQueryProvider)

class TodosView extends LitElement {
  private readonly todos = createQueryController(this, {
    queryKey: ['todos'],
    queryFn: getTodos,
  })

  private readonly createTodo = createMutationController(this, {
    mutationFn: addTodo,
    onSuccess: async () => {
      await queryClient.invalidateQueries({ queryKey: ['todos'] })
    },
  })

  render() {
    const query = this.todos()
    const mutation = this.createTodo()

    if (query.isPending) return html`Loading...`
    if (query.isError) return html`Error: ${query.error.message}`

    return html`
      <ul>
        ${query.data.map((todo) => html`<li>${todo.title}</li>`)}
      </ul>

      <button
        ?disabled=${mutation.isPending}
        @click=${() => this.createTodo.mutate({ title: 'Write Lit docs' })}
      >
        Add Todo
      </button>
    `
  }
}

customElements.define('todos-view', TodosView)

Mount the provider around your component:

<app-query-provider>
  <todos-view></todos-view>
</app-query-provider>

The controllers are created with this because a LitElement is a ReactiveControllerHost. Lit Query uses the host lifecycle to subscribe, request updates, and clean up when the element disconnects.

Continue with Reactive Controllers vs Hooks if you know React Query, or go straight to Queries.