MCPcopy Index your code
hub / github.com/TanStack/table / useTable

Function useTable

examples/vanilla/sorting/src/useTable.ts:17–57  ·  view source on GitHub ↗
(
  options: TableOptions<TData>,
)

Source from the content-addressed store, hash-verified

15}
16
17export const useTable = <TData extends RowData>(
18 options: TableOptions<TData>,
19) => {
20 // Compose in the generic options to the user options
21 const resolvedOptions: TableOptionsResolved<TData> = {
22 state: {}, // Dummy state
23 onStateChange: () => {}, // noop
24 renderFallbackValue: null,
25 ...options,
26 }
27
28 // Create a new table
29 const table = createTable<TData>(resolvedOptions)
30
31 // By default, manage table state here using the table's initial state
32 const state = atom(table.initialState)
33
34 // Subscribe to state changes
35 state.subscribe((currentState) => {
36 table.setOptions((prev) => ({
37 ...prev,
38 ...options,
39 state: {
40 ...currentState,
41 ...options.state,
42 },
43 // Similarly, we'll maintain both our internal state and any user-provided state
44 onStateChange: (updater) => {
45 if (typeof updater === 'function') {
46 const newState = updater(currentState)
47 state.set(newState)
48 } else {
49 state.set(updater)
50 }
51 options.onStateChange?.(updater)
52 },
53 }))
54 })
55
56 return table
57}

Callers 1

main.tsFile · 0.90

Calls 1

createTableFunction · 0.90

Tested by

no test coverage detected