|
| 1 | +--- |
| 2 | +title: Table State (Lit) Guide |
| 3 | +--- |
| 4 | + |
| 5 | +## Table State (Lit) Guide |
| 6 | + |
| 7 | +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. |
| 8 | + |
| 9 | +### Accessing Table State |
| 10 | + |
| 11 | +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. |
| 12 | + |
| 13 | +```ts |
| 14 | +private tableController = new TableController<Person>(this); |
| 15 | + |
| 16 | +render() { |
| 17 | + const table = this.tableController.table({ |
| 18 | + columns, |
| 19 | + data, |
| 20 | + ... |
| 21 | + }) |
| 22 | + |
| 23 | + console.log(table.getState()) //access the entire internal state |
| 24 | + console.log(table.getState().rowSelection) //access just the row selection state |
| 25 | + // ... |
| 26 | +} |
| 27 | +``` |
| 28 | + |
| 29 | +### Custom Initial State |
| 30 | + |
| 31 | +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. |
| 32 | + |
| 33 | +```ts |
| 34 | +render() { |
| 35 | + const table = this.tableController.table({ |
| 36 | + columns, |
| 37 | + data, |
| 38 | + initialState: { |
| 39 | + columnOrder: ['age', 'firstName', 'lastName'], //customize the initial column order |
| 40 | + columnVisibility: { |
| 41 | + id: false //hide the id column by default |
| 42 | + }, |
| 43 | + expanded: true, //expand all rows by default |
| 44 | + sorting: [ |
| 45 | + { |
| 46 | + id: 'age', |
| 47 | + desc: true //sort by age in descending order by default |
| 48 | + } |
| 49 | + ] |
| 50 | + }, |
| 51 | + }) |
| 52 | + |
| 53 | + return html`...`; |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +> **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 take overwrite any corresponding value in `initialState`. |
| 58 | +
|
| 59 | +### Controlled State |
| 60 | + |
| 61 | +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. |
| 62 | + |
| 63 | +#### Individual Controlled State |
| 64 | + |
| 65 | +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. |
| 66 | + |
| 67 | +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. |
| 68 | + |
| 69 | +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. |
| 70 | + |
| 71 | +```jsx |
| 72 | +import {html} from "lit"; |
| 73 | + |
| 74 | +@customElement('my-component') |
| 75 | +class MyComponent extends LitElement { |
| 76 | + @state() |
| 77 | + private _sorting: SortingState = [] |
| 78 | + |
| 79 | + render() { |
| 80 | + const table = this.tableController.table({ |
| 81 | + columns, |
| 82 | + data, |
| 83 | + state: { |
| 84 | + sorting: this._sorting, |
| 85 | + }, |
| 86 | + onSortingChange: updaterOrValue => { |
| 87 | + if (typeof updaterOrValue === 'function') { |
| 88 | + this._sorting = updaterOrValue(this._sorting) |
| 89 | + } else { |
| 90 | + this._sorting = updaterOrValue |
| 91 | + } |
| 92 | + }, |
| 93 | + getSortedRowModel: getSortedRowModel(), |
| 94 | + getCoreRowModel: getCoreRowModel(), |
| 95 | + }) |
| 96 | + |
| 97 | + return html`...` |
| 98 | + } |
| 99 | +} |
| 100 | +//... |
| 101 | +``` |
| 102 | + |
| 103 | +#### Fully Controlled State |
| 104 | + |
| 105 | +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 component tree, like `columnSizingInfo` state`, might cause bad performance issues. |
| 106 | + |
| 107 | +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. |
| 108 | + |
| 109 | +```ts |
| 110 | + |
| 111 | +private tableController = new TableController<Person>(this); |
| 112 | + |
| 113 | +@state() |
| 114 | +private _tableState; |
| 115 | + |
| 116 | +render() { |
| 117 | + const table = this.tableController.table({ |
| 118 | + columns, |
| 119 | + data, |
| 120 | + getCoreRowModel: getCoreRowModel(), |
| 121 | + getSortedRowModel: getSortedRowModel() |
| 122 | + }) |
| 123 | + const state = { ...table.initialState, ...this._tableState }; |
| 124 | + table.setOptions(prev => ({ |
| 125 | + ...prev, |
| 126 | + state, |
| 127 | + onStateChange: updater => { |
| 128 | + this._tableState = |
| 129 | + updater instanceof Function ? updater(state) : updater //any state changes will be pushed up to our own state management |
| 130 | + }, |
| 131 | + })) |
| 132 | + |
| 133 | + return html`...`; |
| 134 | +} |
| 135 | +``` |
| 136 | + |
| 137 | +### On State Change Callbacks |
| 138 | + |
| 139 | +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 these using these options that you should be aware of. |
| 140 | + |
| 141 | +#### 1. **State Change Callbacks MUST have their corresponding state value in the `state` option**. |
| 142 | + |
| 143 | +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. |
| 144 | + |
| 145 | +```jsx |
| 146 | +@state() |
| 147 | +private _sorting = []; |
| 148 | +//... |
| 149 | +render() { |
| 150 | + const table = this.tableController.table({ |
| 151 | + columns, |
| 152 | + data, |
| 153 | + state: { |
| 154 | + sorting: this._sorting, |
| 155 | + }, |
| 156 | + onSortingChange: updaterOrValue => { |
| 157 | + if (typeof updaterOrValue === 'function') { |
| 158 | + this._sorting = updaterOrValue(this._sorting) |
| 159 | + } else { |
| 160 | + this._sorting = updaterOrValue |
| 161 | + } |
| 162 | + }, |
| 163 | + getSortedRowModel: getSortedRowModel(), |
| 164 | + getCoreRowModel: getCoreRowModel(), |
| 165 | + }) |
| 166 | + |
| 167 | + return html`...`; |
| 168 | +} |
| 169 | +``` |
| 170 | + |
| 171 | +#### 2. **Updaters can either be raw values or callback functions**. |
| 172 | + |
| 173 | +The `on[State]Change` and `onStateChange` callbacks work exactly like the `setState` functions in React. 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. |
| 174 | + |
| 175 | +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. |
| 176 | + |
| 177 | +This is why you will see the `updater instanceof Function ? updater(state.value) : updater` pattern in the examples above. This pattern checks if the updater is a function, and if it is, it calls the function with the previous state value to get the new state value. |
| 178 | + |
| 179 | +### State Types |
| 180 | + |
| 181 | +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. |
| 182 | + |
| 183 | +```tsx |
| 184 | +import { TableController, type SortingState } from '@tanstack/lit-table' |
| 185 | +//... |
| 186 | +@state() |
| 187 | +private _sorting: SortingState = [ |
| 188 | + { |
| 189 | + id: 'age', //you should get autocomplete for the `id` and `desc` properties |
| 190 | + desc: true, |
| 191 | + } |
| 192 | +] |
| 193 | +``` |
0 commit comments