@tanstack/react-table 9.0.0-alpha.19 → 9.0.0-alpha.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,9 +1,10 @@
1
1
  "use client";
2
- import { useState, useEffect, useMemo } from "react";
2
+ import { useState, useLayoutEffect, useEffect, useMemo } from "react";
3
3
  import { constructTable } from "@tanstack/table-core";
4
4
  import { useStore, shallow } from "@tanstack/react-store";
5
5
  import { FlexRender } from "./FlexRender.js";
6
6
  import { Subscribe } from "./Subscribe.js";
7
+ const useIsomorphicLayoutEffect = typeof window !== "undefined" ? useLayoutEffect : useEffect;
7
8
  function useTable(tableOptions, selector = () => ({})) {
8
9
  const [table] = useState(() => {
9
10
  const tableInstance = constructTable(tableOptions);
@@ -13,12 +14,24 @@ function useTable(tableOptions, selector = () => ({})) {
13
14
  tableInstance.FlexRender = FlexRender;
14
15
  return tableInstance;
15
16
  });
16
- useEffect(() => {
17
- table.setOptions((prev) => ({
18
- ...prev,
19
- ...tableOptions
20
- }));
21
- }, [table, tableOptions]);
17
+ table.setOptions((prev) => ({
18
+ ...prev,
19
+ ...tableOptions
20
+ }));
21
+ useIsomorphicLayoutEffect(() => {
22
+ queueMicrotask(() => {
23
+ table.baseStore.setState((prev) => ({
24
+ ...prev
25
+ }));
26
+ });
27
+ }, [
28
+ table.options.columns,
29
+ // re-render when columns change
30
+ table.options.data,
31
+ // re-render when data changes
32
+ table.options.state
33
+ // sync react state to the table store
34
+ ]);
22
35
  const state = useStore(table.store, selector, shallow);
23
36
  return useMemo(
24
37
  () => ({
@@ -1 +1 @@
1
- {"version":3,"file":"useTable.js","sources":["../../src/useTable.ts"],"sourcesContent":["'use client'\n\nimport { useEffect, useLayoutEffect, useMemo, useState } from 'react'\nimport { constructTable } from '@tanstack/table-core'\nimport { shallow, useStore } from '@tanstack/react-store'\nimport { FlexRender } from './FlexRender'\nimport { Subscribe } from './Subscribe'\nimport type {\n CellData,\n NoInfer,\n RowData,\n Table,\n TableFeatures,\n TableOptions,\n TableState,\n} from '@tanstack/table-core'\nimport type { FunctionComponent, ReactNode } from 'react'\nimport type { FlexRenderProps } from './FlexRender'\nimport type { SubscribeProps } from './Subscribe'\n\nconst useIsomorphicLayoutEffect =\n typeof window !== 'undefined' ? useLayoutEffect : useEffect\n\nexport type ReactTable<\n TFeatures extends TableFeatures,\n TData extends RowData,\n TSelected = {},\n> = Table<TFeatures, TData> & {\n /**\n * A React HOC (Higher Order Component) that allows you to subscribe to the table state.\n *\n * This is useful for opting into state re-renders for specific parts of the table state.\n *\n * @example\n * <table.Subscribe selector={(state) => ({ rowSelection: state.rowSelection })}>\n * {({ rowSelection }) => ( // important to include `{() => {()}}` syntax\n * <tr key={row.id}>\n // render the row\n * </tr>\n * ))}\n * </table.Subscribe>\n */\n Subscribe: <TSelected>(props: {\n selector: (state: NoInfer<TableState<TFeatures>>) => TSelected\n children: ((state: TSelected) => ReactNode) | ReactNode\n }) => ReturnType<FunctionComponent>\n /**\n * A React component that renders headers, cells, or footers with custom markup.\n * Use this utility component instead of manually calling flexRender.\n *\n * @example\n * ```tsx\n * <table.FlexRender cell={cell} />\n * <table.FlexRender header={header} />\n * <table.FlexRender footer={footer} />\n * ```\n *\n * This replaces calling `flexRender` directly like this:\n * ```tsx\n * flexRender(cell.column.columnDef.cell, cell.getContext())\n * flexRender(header.column.columnDef.header, header.getContext())\n * flexRender(footer.column.columnDef.footer, footer.getContext())\n * ```\n */\n FlexRender: <TValue extends CellData = CellData>(\n props: FlexRenderProps<TFeatures, TData, TValue>,\n ) => ReactNode\n /**\n * The selected state of the table. This state may not match the structure of `table.store.state` because it is selected by the `selector` function that you pass as the 2nd argument to `useTable`.\n *\n * @example\n * const table = useTable(options, (state) => ({ globalFilter: state.globalFilter })) // only globalFilter is part of the selected state\n *\n * console.log(table.state.globalFilter)\n */\n readonly state: Readonly<TSelected>\n}\n\nexport function useTable<\n TFeatures extends TableFeatures,\n TData extends RowData,\n TSelected = {},\n>(\n tableOptions: TableOptions<TFeatures, TData>,\n selector: (state: TableState<TFeatures>) => TSelected = () =>\n ({}) as TSelected,\n): ReactTable<TFeatures, TData, TSelected> {\n const [table] = useState(() => {\n const tableInstance = constructTable(tableOptions) as ReactTable<\n TFeatures,\n TData,\n TSelected\n >\n\n tableInstance.Subscribe = function SubscribeBound<TSelected>(\n props: Omit<SubscribeProps<TFeatures, TData, TSelected>, 'table'>,\n ) {\n return Subscribe({ ...props, table: tableInstance })\n }\n\n tableInstance.FlexRender = FlexRender\n\n return tableInstance\n })\n\n useEffect(() => {\n // sync table options on every render\n table.setOptions((prev) => ({\n ...prev,\n ...tableOptions,\n }))\n }, [table, tableOptions])\n\n const state = useStore(table.store, selector, shallow)\n\n return useMemo(\n () => ({\n ...table,\n state,\n }),\n [state, table],\n )\n}\n"],"names":[],"mappings":";;;;;;AA8EO;AASL;AACE;AAMA;AAGE;AAAmD;AAGrD;AAEA;AAAO;AAGT;AAEE;AAA4B;AACvB;AACA;AACH;AAGJ;AAEA;AAAO;AACE;AACF;AACH;AAAA;AAEW;AAEjB;;;;"}
1
+ {"version":3,"file":"useTable.js","sources":["../../src/useTable.ts"],"sourcesContent":["'use client'\n\nimport { useEffect, useLayoutEffect, useMemo, useState } from 'react'\nimport { constructTable } from '@tanstack/table-core'\nimport { shallow, useStore } from '@tanstack/react-store'\nimport { FlexRender } from './FlexRender'\nimport { Subscribe } from './Subscribe'\nimport type {\n CellData,\n NoInfer,\n RowData,\n Table,\n TableFeatures,\n TableOptions,\n TableState,\n} from '@tanstack/table-core'\nimport type { FunctionComponent, ReactNode } from 'react'\nimport type { FlexRenderProps } from './FlexRender'\nimport type { SubscribeProps } from './Subscribe'\n\nconst useIsomorphicLayoutEffect =\n typeof window !== 'undefined' ? useLayoutEffect : useEffect\n\nexport type ReactTable<\n TFeatures extends TableFeatures,\n TData extends RowData,\n TSelected = {},\n> = Table<TFeatures, TData> & {\n /**\n * A React HOC (Higher Order Component) that allows you to subscribe to the table state.\n *\n * This is useful for opting into state re-renders for specific parts of the table state.\n *\n * @example\n * <table.Subscribe selector={(state) => ({ rowSelection: state.rowSelection })}>\n * {({ rowSelection }) => ( // important to include `{() => {()}}` syntax\n * <tr key={row.id}>\n // render the row\n * </tr>\n * ))}\n * </table.Subscribe>\n */\n Subscribe: <TSelected>(props: {\n selector: (state: NoInfer<TableState<TFeatures>>) => TSelected\n children: ((state: TSelected) => ReactNode) | ReactNode\n }) => ReturnType<FunctionComponent>\n /**\n * A React component that renders headers, cells, or footers with custom markup.\n * Use this utility component instead of manually calling flexRender.\n *\n * @example\n * ```tsx\n * <table.FlexRender cell={cell} />\n * <table.FlexRender header={header} />\n * <table.FlexRender footer={footer} />\n * ```\n *\n * This replaces calling `flexRender` directly like this:\n * ```tsx\n * flexRender(cell.column.columnDef.cell, cell.getContext())\n * flexRender(header.column.columnDef.header, header.getContext())\n * flexRender(footer.column.columnDef.footer, footer.getContext())\n * ```\n */\n FlexRender: <TValue extends CellData = CellData>(\n props: FlexRenderProps<TFeatures, TData, TValue>,\n ) => ReactNode\n /**\n * The selected state of the table. This state may not match the structure of `table.store.state` because it is selected by the `selector` function that you pass as the 2nd argument to `useTable`.\n *\n * @example\n * const table = useTable(options, (state) => ({ globalFilter: state.globalFilter })) // only globalFilter is part of the selected state\n *\n * console.log(table.state.globalFilter)\n */\n readonly state: Readonly<TSelected>\n}\n\nexport function useTable<\n TFeatures extends TableFeatures,\n TData extends RowData,\n TSelected = {},\n>(\n tableOptions: TableOptions<TFeatures, TData>,\n selector: (state: TableState<TFeatures>) => TSelected = () =>\n ({}) as TSelected,\n): ReactTable<TFeatures, TData, TSelected> {\n const [table] = useState(() => {\n const tableInstance = constructTable(tableOptions) as ReactTable<\n TFeatures,\n TData,\n TSelected\n >\n\n tableInstance.Subscribe = function SubscribeBound<TSelected>(\n props: Omit<SubscribeProps<TFeatures, TData, TSelected>, 'table'>,\n ) {\n return Subscribe({ ...props, table: tableInstance })\n }\n\n tableInstance.FlexRender = FlexRender\n\n return tableInstance\n })\n\n // sync table options on every render\n table.setOptions((prev) => ({\n ...prev,\n ...tableOptions,\n }))\n\n useIsomorphicLayoutEffect(() => {\n // prevent race condition between table.setOptions and table.baseStore.setState\n queueMicrotask(() => {\n table.baseStore.setState((prev) => ({\n ...prev,\n }))\n })\n }, [\n table.options.columns, // re-render when columns change\n table.options.data, // re-render when data changes\n table.options.state, // sync react state to the table store\n ])\n\n const state = useStore(table.store, selector, shallow)\n\n return useMemo(\n () => ({\n ...table,\n state,\n }),\n [state, table],\n )\n}\n"],"names":[],"mappings":";;;;;;AAoBA;AA0DO;AASL;AACE;AAMA;AAGE;AAAmD;AAGrD;AAEA;AAAO;AAIT;AAA4B;AACvB;AACA;AAGL;AAEE;AACE;AAAoC;AAC/B;AACH;AACH;AACA;AACa;AAAA;AACA;AAAA;AACA;AAAA;AAGhB;AAEA;AAAO;AACE;AACF;AACH;AAAA;AAEW;AAEjB;;;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/react-table",
3
- "version": "9.0.0-alpha.19",
3
+ "version": "9.0.0-alpha.20",
4
4
  "description": "Headless UI for building powerful tables & datagrids for React.",
5
5
  "author": "Tanner Linsley",
6
6
  "license": "MIT",
package/src/useTable.ts CHANGED
@@ -103,13 +103,24 @@ export function useTable<
103
103
  return tableInstance
104
104
  })
105
105
 
106
- useEffect(() => {
107
- // sync table options on every render
108
- table.setOptions((prev) => ({
109
- ...prev,
110
- ...tableOptions,
111
- }))
112
- }, [table, tableOptions])
106
+ // sync table options on every render
107
+ table.setOptions((prev) => ({
108
+ ...prev,
109
+ ...tableOptions,
110
+ }))
111
+
112
+ useIsomorphicLayoutEffect(() => {
113
+ // prevent race condition between table.setOptions and table.baseStore.setState
114
+ queueMicrotask(() => {
115
+ table.baseStore.setState((prev) => ({
116
+ ...prev,
117
+ }))
118
+ })
119
+ }, [
120
+ table.options.columns, // re-render when columns change
121
+ table.options.data, // re-render when data changes
122
+ table.options.state, // sync react state to the table store
123
+ ])
113
124
 
114
125
  const state = useStore(table.store, selector, shallow)
115
126