跳到正文
Skillpedia
网站与界面(前端)29k · #9815

table-state

它能做什么

这项技能解释如何在 Lit 组件中保存表格设置,例如当前页,以及表格如何对它们作出响应。适合需要视图与设置保持同步的 Lit 开发者。

适用场景

  • ✓当 Lit 模板必须显示当前页或其他表格设置的时候。
  • ✓当你需要把组件中的表格设置传入表格的时候。
  • ✓当表格设置变化后,Lit 视图没有更新的时候。

技能链接

https://github.com/TanStack/table/blob/main/packages/lit-table/skills/table-state/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/TanStack/table/blob/main/packages/lit-table/skills/table-state/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“点击“下一页”后,如何更新我的 Lit 表格视图中的页码?”

每周 5 个新技能,1 分钟读完。

工作原理

它展示如何把设置保存为 Lit 响应式属性,将其传给表格,并把更新反馈回来,使组件重新渲染。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

Load intent load @tanstack/table-core#table-state first for shared ownership, initialization, updater, and reset rules.

TableController selects all registered state by default and requests host updates when selected state changes. table.state is the selected render value. table.atoms.<slice>.get() and table.store.state are snapshots; those reads do not create a template subscription.

Controlled property setup

<!-- skill-snippet:check tsconfig=examples/lit/basic-table-controller/tsconfig.json -->
import { LitElement, html } from 'lit'
import { customElement, state } from 'lit/decorators.js'
import {
  TableController,
  rowPaginationFeature,
  tableFeatures,
  type PaginationState,
} from '@tanstack/lit-table'

const features = tableFeatures({ rowPaginationFeature })
const columns = [{ accessorKey: 'id' }]
const data = [{ id: '1' }]

@customElement('page-status')
export class PageStatus extends LitElement {
  @state() private pagination: PaginationState = { pageIndex: 0, pageSize: 10 }
  private controller = new TableController<typeof features, { id: string }>(
    this,
  )

  protected render() {
    const table = this.controller.table({
      features,
      columns,
      data,
      state: { pagination: this.pagination },
      onPaginationChange: (next) => {
        this.pagination =
          typeof next === 'function' ? next(this.pagination) : next
      },
    })
    return html`<button @click=${() => table.nextPage()}>
      Page ${table.state.pagination.pageIndex + 1}
    </button>`
  }
}

Reactive boundaries

  • Keep one controller on the host and pass current options during render. Keep features, columns, and data references stable across host updates.
  • A second controller.table(options, selector) argument changes the shape of table.state and gates host updates. Keep reused selectors stable.
  • Use table.subscribe(source, selector, renderCallback) for a template region that should update independently. Cache selector functions as host fields or module functions.
  • Feed every controlled callback result into the corresponding reactive property and pass it back through state. Use stable external atoms through atoms when sharing state outside the host.

Read for the task

For advanced reactive boundaries, shared atoms, or detailed subscription examples, read reactivity. For construction or rendering setup, read getting-started.

API discovery

Inspect node_modules/@tanstack/lit-table/dist/index.d.ts, then TableController.d.ts, flexRender.d.ts, or the relevant exported declaration. Core feature APIs are in node_modules/@tanstack/table-core/dist/features/.

常见问题

为什么在处理函数中读取的值与在模板中读取的不同?

模板读取会使视图持续订阅变化,而在处理函数中读取的值只是那一刻的快照。

控制器默认选择什么?

默认选择所有已注册的表格状态,并在所选状态变化时请求宿主更新。

我可以限制模板监听的内容吗?

可以,正如指南所述,可以为模板的某个区域使用稳定的选择器。

详情

作者的原始描述

Read and control Table v9 state in lit. Use for tracked reads, subscriptions, controlled slices, and framework-specific reactive boundaries.

repo: TanStack/table

热度

GitHub 星标
29k
排名
#9815
许可证
MIT
更新时间
2026/10/21

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能