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

table-state

它能做什么

这项技能解释如何把表格设置,例如当前页,保存在 React 状态中,以及表格如何读取它们。适合视图必须与表格状态保持一致的 React 开发者。

适用场景

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

技能链接

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

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

使用方法

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

可直接粘贴的消息

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

可以这样问 AI

“点击“下一页”按钮后,如何更新我的 React 表格中的页码?”

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

工作原理

它展示如何用 React 状态钩子保存设置,用对应的更新回调将其传给表格,并读取所选状态,使视图重新渲染。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

React 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 React subscriptions.

Use selected table.state in the owner render. For narrower boundaries, use table.Subscribe or useSelector from @tanstack/react-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 'react'
import {
  rowPaginationFeature,
  tableFeatures,
  useTable,
  type PaginationState,
} from '@tanstack/react-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 React 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/react-store, pass it in atoms.<slice>, and subscribe with useSelector where other UI needs its value.

Reactive boundaries

For React Compiler memoized children, put Subscribe inside the component hiding builder-method reads or pass the selected value as a changing prop. An outer subscription that ignores its value cannot refresh a stable child. In core-typed cell/header contexts, import standalone Subscribe.

For external-atom examples, selector dependencies, or fine-grained boundaries and React Compiler problems, 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/react-table/dist/useTable.d.ts and Subscribe.d.ts, plus the installed @tanstack/react-store declarations for atom hooks.

常见问题

为什么在事件处理函数中读取的值看起来过时了?

在处理函数中读取的值是快照,因此视图只能通过所选状态或订阅来更新。

值和回调我都需要吗?

是的,这项技能说明应把每个受控值与其更新回调一起传入,这样变化才能正确地回流。

我可以缩小重新渲染的范围吗?

可以,存在更窄的订阅方式,但这项技能建议在渲染成本需要缩小范围之前,使用默认设置。

详情

作者的原始描述

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

repo: TanStack/table

热度

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

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能