table-state
它能做什么
这项技能解释如何把表格设置,例如当前页,保存在 Svelte runes 中,并在模板中读取它们。适合表格与页面必须保持同步的 Svelte 开发者。
适用场景
- ✓当 Svelte 模板必须显示当前页或其他表格设置的时候。
- ✓当你需要把表格的分页连接到一个 rune 的时候。
- ✓当在模板或派生代码之外读取的值在变化后没有更新的时候。
技能链接
https://github.com/TanStack/table/blob/main/packages/svelte-table/skills/table-state/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“如何让我的 Svelte 表格在点击“下一页”时在屏幕上更新页码?”
每周 5 个新技能,1 分钟读完。
工作原理
它展示如何把分页设置保存在一个 rune 中,通过 getter 和更新回调将其传给表格,并在模板或派生值中读取状态。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Svelte table state
Before starting, run intent load @tanstack/table-core#table-state for shared ownership, initialization, updates, and resets.
Read reactive state
In Svelte 5, table.atoms.<slice>.get(), table.store.get(), and Table API reads track dependencies inside templates, $derived, $derived.by, or $effect. Outside those contexts they return snapshots. Prefer a slice read; a whole-store read reruns for every registered state change.
createTable and createAppTable take only options. There is no selected table.state, subscribeTable, or SubscribeSource. Use native $derived projections.
Control a slice
Keep features and columns stable. Expose changing data and controlled runes through getters, and resolve both forms of Updater:
<script lang="ts">
import {
createTable,
rowPaginationFeature,
tableFeatures,
type PaginationState,
} from '@tanstack/svelte-table'
const features = tableFeatures({ rowPaginationFeature })
const columns = [{ accessorKey: 'name' }]
let data = $state([{ name: 'Ada' }])
let pagination = $state<PaginationState>({ pageIndex: 0, pageSize: 20 })
const table = createTable({
features,
columns,
get data() {
return data
},
state: {
get pagination() {
return pagination
},
},
onPaginationChange: (next) => {
pagination = typeof next === 'function' ? next(pagination) : next
},
})
const pageSize = $derived(table.atoms.pagination.get().pageSize)
</script>
<button onclick={() => table.setPageSize(50)}>{pageSize} rows per page</button>
createTableState provides an updater-compatible getter/setter pair when repeating this wiring. For raw external atoms consumed outside table UI, use useSelector from @tanstack/svelte-store; table UI uses the rune-aware table.atoms wrappers.
For createTableState, external atoms, broad invalidation, or a page index overwritten by automatic reset, read reactivity details. For changes to pagination processing or reset policy, run intent load @tanstack/table-core#table-features and read its pagination reference.
API discovery
Inspect node_modules/@tanstack/svelte-table/dist/createTable.svelte.d.ts, createTableState.svelte.d.ts, and createTableHook.svelte.d.ts for the installed contracts.
常见问题
为什么在普通函数中读取的值不刷新?
Svelte 只在模板、派生值或副作用中追踪读取,其他地方的读取都是快照。
Svelte 中有所选状态对象吗?
没有,这项技能说明 createTable 只接受选项,因此投影应使用原生的派生值。
我应该读取整个 store 还是只读取一个片段?
这项技能建议只读取单个片段,因为读取整个 store 时,每次注册的状态变化都会重新运行。
详情
作者的原始描述
Read and control Table v9 state in svelte. Use for tracked reads, subscriptions, controlled slices, and framework-specific reactive boundaries.
repo: TanStack/table
热度
- GitHub 星标
- 29k
- 排名
- #9833
- 许可证
- 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 设置、模板渲染和行数据的更新。