Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,15 @@
> - [Angular Table](https://tanstack.com/table/v8/docs/framework/angular/angular-table)
> - [Lit Table](https://tanstack.com/table/v8/docs/framework/lit/lit-table)
> - [Qwik Table](https://tanstack.com/table/v8/docs/framework/qwik/qwik-table)
> - [Preact Table](https://tanstack.com/table/v8/docs/framework/preact/preact-table)
> - [React Table](https://tanstack.com/table/v8/docs/framework/react/react-table)
> - [Solid Table](https://tanstack.com/table/v8/docs/framework/solid/solid-table)
> - [Svelte Table](https://tanstack.com/table/v8/docs/framework/svelte/svelte-table)
> - [Vue Table](https://tanstack.com/table/v8/docs/framework/vue/vue-table)

A headless table library for building powerful datagrids with full control over markup, styles, and behavior.

- Framework‑agnostic core with bindings for React, Vue & Solid
- Framework‑agnostic core with bindings for React, Preact, Vue, Solid, and more
- 100% customizable — bring your own UI, components, and styles
- Sorting, filtering, grouping, aggregation & row selection
- Lightweight, virtualizable & server‑side friendly
Expand Down
18 changes: 18 additions & 0 deletions docs/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@
}
]
},
{
"label": "preact",
"children": [
{
"label": "Preact Table Adapter",
"to": "framework/preact/preact-table"
}
]
},
{
"label": "react",
"children": [
Expand Down Expand Up @@ -173,6 +182,15 @@
}
]
},
{
"label": "preact",
"children": [
{
"label": "Table State",
"to": "framework/preact/guide/table-state"
}
]
},
{
"label": "react",
"children": [
Expand Down
196 changes: 196 additions & 0 deletions docs/framework/preact/guide/table-state.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
---
title: Table State (Preact) Guide
---

## Table State (Preact) Guide

TanStack Table has a simple underlying internal state management system to store and manage the state of the table. It also lets you selectively pull out any state that you need to manage in your own state management. This guide will walk you through the different ways in which you can interact with and manage the state of the table.

### Accessing Table State

You do not need to set up anything special in order for the table state to work. If you pass nothing into either `state`, `initialState`, or any of the `on[State]Change` table options, the table will manage its own state internally. You can access any part of this internal state by using the `table.getState()` table instance API.

```jsx
const table = usePreactTable({
columns,
data,
//...
})

console.log(table.getState()) //access the entire internal state
console.log(table.getState().rowSelection) //access just the row selection state
```

### Custom Initial State

If all you need to do for certain states is customize their initial default values, you still do not need to manage any of the state yourself. You can simply set values in the `initialState` option of the table instance.

```jsx
const table = usePreactTable({
columns,
data,
initialState: {
columnOrder: ['age', 'firstName', 'lastName'], //customize the initial column order
columnVisibility: {
id: false //hide the id column by default
},
expanded: true, //expand all rows by default
sorting: [
{
id: 'age',
desc: true //sort by age in descending order by default
}
]
},
//...
})
```

> **Note**: Only specify each particular state in either `initialState` or `state`, but not both. If you pass in a particular state value to both `initialState` and `state`, the initialized state in `state` will overwrite any corresponding value in `initialState`.

### Controlled State

If you need easy access to the table state in other areas of your application, TanStack Table makes it easy to control and manage any or all of the table state in your own state management system. You can do this by passing in your own state and state management functions to the `state` and `on[State]Change` table options.

#### Individual Controlled State

You can control just the state that you need easy access to. You do NOT have to control all of the table state if you do not need to. It is recommended to only control the state that you need on a case-by-case basis.

In order to control a particular state, you need to both pass in the corresponding `state` value and the `on[State]Change` function to the table instance.

Let's take filtering, sorting, and pagination as an example in a "manual" server-side data fetching scenario. You can store the filtering, sorting, and pagination state in your own state management, but leave out any other state like column order, column visibility, etc. if your API does not care about those values.

```jsx
const [columnFilters, setColumnFilters] = useState([]) //no default filters
const [sorting, setSorting] = useState([{
id: 'age',
desc: true, //sort by age in descending order by default
}])
const [pagination, setPagination] = useState({ pageIndex: 0, pageSize: 15 })

//Use our controlled state values to fetch data
const tableQuery = useQuery({
queryKey: ['users', columnFilters, sorting, pagination],
queryFn: () => fetchUsers(columnFilters, sorting, pagination),
//...
})

const table = usePreactTable({
columns,
data: tableQuery.data,
//...
state: {
columnFilters, //pass controlled state back to the table (overrides internal state)
sorting,
pagination
},
onColumnFiltersChange: setColumnFilters, //hoist columnFilters state into our own state management
onSortingChange: setSorting,
onPaginationChange: setPagination,
})
//...
```

#### Fully Controlled State

Alternatively, you can control the entire table state with the `onStateChange` table option. It will hoist out the entire table state into your own state management system. Be careful with this approach, as you might find that raising some frequently changing state values up a preact tree, like `columnSizingInfo` state`, might cause bad performance issues.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Typo: stray backtick in prose.

There's a mismatched backtick: `columnSizingInfo` state` — the trailing backtick after "state" is erroneous.

Proposed fix
-A couple of more tricks may be needed to make this work. If you use the `onStateChange` table option, the initial values of the `state` must be populated with all of the relevant state values for all of the features that you want to use. You can either manually type out all of the initial state values, or use the `table.setOptions` API in a special way as shown below.
+raising some frequently changing state values up a preact tree, like `columnSizingInfo` state, might cause bad performance issues.

More precisely, on line 96 fix:

-like `columnSizingInfo` state`, might cause bad performance issues.
+like `columnSizingInfo` state, might cause bad performance issues.
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
Alternatively, you can control the entire table state with the `onStateChange` table option. It will hoist out the entire table state into your own state management system. Be careful with this approach, as you might find that raising some frequently changing state values up a preact tree, like `columnSizingInfo` state`, might cause bad performance issues.
Alternatively, you can control the entire table state with the `onStateChange` table option. It will hoist out the entire table state into your own state management system. Be careful with this approach, as you might find that raising some frequently changing state values up a preact tree, like `columnSizingInfo` state, might cause bad performance issues.
🧰 Tools
🪛 LanguageTool

[grammar] ~96-~96: Ensure spelling is correct
Context: ...e frequently changing state values up a preact tree, like columnSizingInfo state`, m...

(QB_NEW_EN_ORTHOGRAPHY_ERROR_IDS_1)

🤖 Prompt for AI Agents
In `@docs/framework/preact/guide/table-state.md` at line 96, The sentence contains
a stray trailing backtick after "state"; update the prose so the mention of the
column sizing state is correctly formatted (e.g., `columnSizingInfo` state) by
removing the extra backtick; this change applies near the mention of the
onStateChange table option and the symbol columnSizingInfo in the documentation.


A couple of more tricks may be needed to make this work. If you use the `onStateChange` table option, the initial values of the `state` must be populated with all of the relevant state values for all of the features that you want to use. You can either manually type out all of the initial state values, or use the `table.setOptions` API in a special way as shown below.

```jsx
//create a table instance with default state values
const table = usePreactTable({
columns,
data,
//... Note: `state` values are NOT passed in yet
})

const [state, setState] = useState({
...table.initialState, //populate the initial state with all of the default state values from the table instance
pagination: {
pageIndex: 0,
pageSize: 15 //optionally customize the initial pagination state.
}
})

//Use the table.setOptions API to merge our fully controlled state onto the table instance
table.setOptions(prev => ({
...prev, //preserve any other options that we have set up above
state, //our fully controlled state overrides the internal state
onStateChange: setState //any state changes will be pushed up to our own state management
}))
```

### On State Change Callbacks

So far, we have seen the `on[State]Change` and `onStateChange` table options work to "hoist" the table state changes into our own state management. However, there are a few things about using these options that you should be aware of.

#### 1. **State Change Callbacks MUST have their corresponding state value in the `state` option**.

Specifying an `on[State]Change` callback tells the table instance that this will be a controlled state. If you do not specify the corresponding `state` value, that state will be "frozen" with its initial value.

```jsx
const [sorting, setSorting] = useState([])
//...
const table = usePreactTable({
columns,
data,
//...
state: {
sorting, //required because we are using `onSortingChange`
},
onSortingChange: setSorting, //makes the `state.sorting` controlled
})
```

#### 2. **Updaters can either be raw values or callback functions**.

The `on[State]Change` and `onStateChange` callbacks work exactly like the `setState` functions in React/Preact. The updater values can either be a new state value or a callback function that takes the previous state value and returns the new state value.

What implications does this have? It means that if you want to add in some extra logic in any of the `on[State]Change` callbacks, you can do so, but you need to check whether or not the new incoming updater value is a function or value.

```jsx
const [sorting, setSorting] = useState([])
const [pagination, setPagination] = useState({ pageIndex: 0, pageSize: 10 })

const table = usePreactTable({
columns,
data,
//...
state: {
pagination,
sorting,
}
//syntax 1
onPaginationChange: (updater) => {
setPagination(old => {
const newPaginationValue = updater instanceof Function ? updater(old) : updater
//do something with the new pagination value
//...
return newPaginationValue
})
},
//syntax 2
onSortingChange: (updater) => {
const newSortingValue = updater instanceof Function ? updater(sorting) : updater
//do something with the new sorting value
//...
setSorting(updater) //normal state update
}
})
```

### State Types

All complex states in TanStack Table have their own TypeScript types that you can import and use. This can be handy for ensuring that you are using the correct data structures and properties for the state values that you are controlling.

```tsx
import { usePreactTable, type SortingState } from '@tanstack/preact-table'
//...
const [sorting, setSorting] = useState<SortingState[]>([
{
id: 'age', //you should get autocomplete for the `id` and `desc` properties
desc: true,
}
])
Comment on lines +188 to +195
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Incorrect type usage: SortingState[] should be SortingState.

SortingState is already defined as ColumnSort[] in table-core. Writing useState<SortingState[]> creates a ColumnSort[][] (array of arrays), which is wrong and will cause type errors.

Proposed fix
-const [sorting, setSorting] = useState<SortingState[]>([
+const [sorting, setSorting] = useState<SortingState>([
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
import { usePreactTable, type SortingState } from '@tanstack/preact-table'
//...
const [sorting, setSorting] = useState<SortingState[]>([
{
id: 'age', //you should get autocomplete for the `id` and `desc` properties
desc: true,
}
])
import { usePreactTable, type SortingState } from '@tanstack/preact-table'
//...
const [sorting, setSorting] = useState<SortingState>([
{
id: 'age', //you should get autocomplete for the `id` and `desc` properties
desc: true,
}
])
🤖 Prompt for AI Agents
In `@docs/framework/preact/guide/table-state.md` around lines 188 - 195, The
example incorrectly types the state as useState<SortingState[]> which produces a
nested array; change the state to useState<SortingState> so sorting is a single
SortingState (alias for ColumnSort[]). Update the variable declaration using
SortingState (and keep setSorting signature) in the example where usePreactTable
and useState are used so autocomplete and types work correctly for sorting.id
and sorting.desc.

```
19 changes: 19 additions & 0 deletions docs/framework/preact/preact-table.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
title: Preact Table
---

The `@tanstack/preact-table` adapter is a wrapper around the core table logic. Most of its job is related to managing state the "preact" way, providing types and the rendering implementation of cell/header/footer templates.

## `usePreactTable`

Takes an `options` object and returns a table.

```tsx
import { usePreactTable } from '@tanstack/preact-table'

function App() {
const table = usePreactTable(options)

// ...render your table
}
```
5 changes: 4 additions & 1 deletion docs/guide/tables.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ title: Table Instance Guide

## Table Instance Guide

TanStack Table is a headless UI library. When we talk about the `table` or "table instance", we're not talking about a literal `<table>` element. Instead, we're referring to the core table object that contains the table state and APIs. The `table` instance is created by calling your adapter's `createTable` function (e.g. `useReactTable`, `createSolidTable`, `createSvelteTable`, `useQwikTable`, `useVueTable`).
TanStack Table is a headless UI library. When we talk about the `table` or "table instance", we're not talking about a literal `<table>` element. Instead, we're referring to the core table object that contains the table state and APIs. The `table` instance is created by calling your adapter's `createTable` function (e.g. `useReactTable`, `usePreactTable`, `createSolidTable`, `createSvelteTable`, `useQwikTable`, `useVueTable`).

### Creating a Table Instance

Expand Down Expand Up @@ -92,6 +92,9 @@ const table = createTable({ columns, data })
//react
const table = useReactTable({ columns, data })

//preact
const table = usePreactTable({ columns, data })

//solid
const table = createSolidTable({ columns, data })

Expand Down
8 changes: 8 additions & 0 deletions docs/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ The `@tanstack/react-table` package works with React 16.8, React 17, React 18, a

> NOTE: Even though the react adapter works with React 19, it may not work with the new React Compiler that's coming out along-side React 19. This may be fixed in future TanStack Table updates.

## Preact Table

```bash
npm install @tanstack/preact-table
```

The `@tanstack/preact-table` package works with Preact 10.

## Vue Table

```bash
Expand Down
2 changes: 1 addition & 1 deletion docs/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: Introduction
---

TanStack Table is a **Headless UI** library for building powerful tables & datagrids for TS/JS, React, Vue, Solid, Qwik, and Svelte.
TanStack Table is a **Headless UI** library for building powerful tables & datagrids for TS/JS, React, Preact, Vue, Solid, Qwik, and Svelte.

## What is "headless" UI?

Expand Down
2 changes: 1 addition & 1 deletion docs/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ While TanStack Table is written in [TypeScript](https://www.typescriptlang.org/)

## Headless

As it was mentioned extensively in the [Intro](./introduction.md) section, TanStack Table is **headless**. This means that it doesn't render any DOM elements, and instead relies on you, the UI/UX developer to provide the table's markup and styles. This is a great way to build a table that can be used in any UI framework, including React, Vue, Solid, Svelte, Qwik, and even JS-to-native platforms like React Native!
As it was mentioned extensively in the [Intro](./introduction.md) section, TanStack Table is **headless**. This means that it doesn't render any DOM elements, and instead relies on you, the UI/UX developer to provide the table's markup and styles. This is a great way to build a table that can be used in any UI framework, including React, Preact, Vue, Solid, Svelte, Qwik, and even JS-to-native platforms like React Native!

## Core Objects and Types

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"test": "pnpm run test:ci",
"test:pr": "nx affected --targets=test:sherif,test:knip,test:docs,test:lib,test:types,build",
"test:ci": "nx run-many --targets=test:sherif,test:knip,test:docs,test:lib,test:types,build",
"test:sherif": "sherif -i react -i react-dom -i vue -i solid-js -i svelte -i @builder.io/qwik",
"test:sherif": "sherif -i preact -i react -i react-dom -i vue -i solid-js -i svelte -i @builder.io/qwik",
"test:lib": "nx affected --targets=test:lib --exclude=examples/**",
"test:lib:dev": "pnpm test:lib && nx watch --all -- pnpm test:lib",
"test:types": "nx affected --targets=test:types --exclude=examples/**",
Expand Down
62 changes: 62 additions & 0 deletions packages/preact-table/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{
"name": "@tanstack/preact-table",
"version": "8.21.3",
"description": "Headless UI for building powerful tables & datagrids for Preact.",
"author": "Tanner Linsley",
"license": "MIT",
"repository": {
"type": "git",
"url": "git+https://github.com/TanStack/table.git",
"directory": "packages/preact-table"
},
"homepage": "https://tanstack.com/table",
"funding": {
"type": "github",
"url": "https://github.com/sponsors/tannerlinsley"
},
"keywords": [
"preact",
"table",
"preact-table",
"datagrid"
],
"type": "commonjs",
"module": "build/lib/index.esm.js",
"main": "build/lib/index.js",
"types": "build/lib/index.d.ts",
"exports": {
".": {
"types": "./build/lib/index.d.ts",
"import": "./build/lib/index.mjs",
"default": "./build/lib/index.js"
},
"./package.json": "./package.json"
},
"sideEffects": false,
"engines": {
"node": ">=12"
},
"files": [
"build/lib/*",
"build/umd/*",
"src"
],
"scripts": {
"clean": "rimraf ./build",
"test:lib": "vitest",
"test:lib:dev": "pnpm test:lib --watch",
"test:types": "tsc --noEmit",
"build": "pnpm build:rollup && pnpm build:types",
"build:rollup": "rollup --config rollup.config.mjs",
"build:types": "tsc --emitDeclarationOnly"
},
"dependencies": {
"@tanstack/table-core": "workspace:*"
},
"devDependencies": {
"preact": "^10.23.2"
},
"peerDependencies": {
"preact": ">=10"
}
}
Loading