table-state
它能做什么
这项技能解释如何把表格设置,例如当前页,保存在 Preact 组件中,以及表格如何读取它们。适合视图必须与表格状态保持一致的 Preact 开发者。
适用场景
- ✓当 Preact 组件必须显示当前页或其他表格设置的时候。
- ✓当你需要把组件中的表格设置传入表格的时候。
- ✓当表格设置变化后,Preact 视图没有更新的时候。
技能链接
https://github.com/TanStack/table/blob/main/packages/preact-table/skills/table-state/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“点击“下一页”后,如何更新我的 Preact 表格中的页码?”
每周 5 个新技能,1 分钟读完。
工作原理
它展示如何用状态钩子保存设置,用对应的更新回调将其传给表格,并读取所选状态,使视图重新渲染。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Preact table state
Before starting, run intent load @tanstack/table-core#table-state for shared ownership, initialization, updates, and resets. State repair does not require loading setup guidance.
Read reactive state
useTable selects all registered state by default. Its optional second-argument selector controls rerenders and the shape of table.state. table.atoms.<slice>.get() and table.store.state are current snapshots, not Preact subscriptions.
Use selected table.state in the owner render. For narrower boundaries, use table.Subscribe or useSelector from @tanstack/preact-store. Without source, table.Subscribe selects from table.store; with source, it subscribes to that atom or store. Start with the default selector until render cost requires narrowing.
Control a slice
Keep features, columns, and data stable. Register the feature that owns the controlled slice. This example controls pagination without adding client row processing:
import { useState } from 'preact/hooks'
import {
rowPaginationFeature,
tableFeatures,
useTable,
type PaginationState,
} from '@tanstack/preact-table'
const features = tableFeatures({ rowPaginationFeature })
const columns = [{ accessorKey: 'name' }]
const data = [{ name: 'Ada' }]
export function PageControl() {
const [pagination, setPagination] = useState<PaginationState>({
pageIndex: 0,
pageSize: 20,
})
const table = useTable({
features,
columns,
data,
state: { pagination },
onPaginationChange: setPagination,
})
return (
<button onClick={() => table.setPageSize(50)}>
{table.state.pagination.pageSize} rows per page
</button>
)
}
The Preact setter accepts both a value and a functional updater. Supply the current state.pagination together with onPaginationChange; a callback alone does not keep the table synchronized.
For an externally owned atom, create it with useCreateAtom from @tanstack/preact-store, pass it in atoms.<slice>, and subscribe with useSelector where other UI needs its value.
Reactive boundaries
Use native @tanstack/preact-store bindings. A narrowed selector must include every state slice used by rendered builder methods, or those consumers need their own subscription.
For external-atom examples, selector dependencies, or fine-grained boundaries, read reactivity details. If this task changes processing features, run intent load @tanstack/table-core#table-features and read the relevant feature references.
API discovery
Inspect node_modules/@tanstack/preact-table/dist/useTable.d.ts and Subscribe.d.ts, plus the installed @tanstack/preact-store declarations for atom hooks.
常见问题
为什么在点击处理函数中读取值不会刷新视图?
在处理函数中读取的值是那一刻的快照,因此视图只能通过所选状态或订阅来刷新。
我需要同时传入回调和值吗?
是的,这项技能说明每个受控值都应与其回调配对,这样变化才能回流到组件。
我可以限制哪些部分的表格重新渲染吗?
可以,指南描述了更窄的订阅方式,但建议在渲染成本足够高之前使用默认设置。
详情
作者的原始描述
Read and control Table v9 state in preact. Use for tracked reads, subscriptions, controlled slices, and framework-specific reactive boundaries.
repo: TanStack/table
热度
- GitHub 星标
- 29k
- 排名
- #9822
- 许可证
- MIT
- 更新时间
- 2026/10/21
评论
- …
同一仓库的更多技能
getting-started
29k教你的智能体如何在 Alpine.js 中使用 TanStack Table 构建并显示数据表格,涵盖设置、响应式数据和渲染行。
table-state
29k教你的智能体如何在 Alpine.js 中读取和控制表格状态,例如当前页码,使状态变化时屏幕能够更新。
devtools
29k教你的智能体如何把 Angular 表格注册到 TanStack Devtools 面板中,使开发者在构建应用时能检查每个表格。
getting-started
29k教你的智能体如何在 Angular 中创建并显示 TanStack 表格,涵盖 injectTable 设置、模板渲染和行数据的更新。