Skip to content

Commit 0691785

Browse files
committed
fix: put columns and data in useStore selector in useTable
1 parent 82107df commit 0691785

File tree

2 files changed

+26
-38
lines changed

2 files changed

+26
-38
lines changed

packages/preact-table/src/useTable.ts

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useEffect, useLayoutEffect, useMemo, useState } from 'preact/hooks'
1+
import { useMemo, useState } from 'preact/hooks'
22
import { constructTable } from '@tanstack/table-core'
33
import { shallow, useStore } from '@tanstack/preact-store'
44
import { FlexRender } from './FlexRender'
@@ -16,9 +16,6 @@ import type { ComponentChildren } from 'preact'
1616
import type { FlexRenderProps } from './FlexRender'
1717
import type { SubscribeProps } from './Subscribe'
1818

19-
const useIsomorphicLayoutEffect =
20-
typeof window !== 'undefined' ? useLayoutEffect : useEffect
21-
2219
export type PreactTable<
2320
TFeatures extends TableFeatures,
2421
TData extends RowData,
@@ -71,7 +68,10 @@ export type PreactTable<
7168
*
7269
* console.log(table.state.globalFilter)
7370
*/
74-
readonly state: Readonly<TSelected>
71+
readonly state: Readonly<TSelected> & {
72+
columns: TableOptions<TFeatures, TData>['columns']
73+
data: TableOptions<TFeatures, TData>['data']
74+
}
7575
}
7676

7777
export function useTable<
@@ -107,26 +107,21 @@ export function useTable<
107107
...tableOptions,
108108
}))
109109

110-
useIsomorphicLayoutEffect(() => {
111-
// prevent race condition between table.setOptions and table.baseStore.setState
112-
queueMicrotask(() => {
113-
table.baseStore.setState((prev) => ({
114-
...prev,
115-
}))
116-
})
117-
}, [
118-
table.options.columns, // re-render when columns change
119-
table.options.data, // re-render when data changes
120-
table.options.state, // sync preact state to the table store
121-
])
110+
const selectorWithDataAndColumns = (state: TableState<TFeatures>) => ({
111+
columns: tableOptions.columns,
112+
data: tableOptions.data,
113+
...selector(state),
114+
})
122115

123-
const state = useStore(table.store, selector, { equal: shallow })
116+
const state = useStore(table.store, selectorWithDataAndColumns, {
117+
equal: shallow,
118+
})
124119

125120
return useMemo(
126121
() => ({
127122
...table,
128123
state,
129124
}),
130125
[state, table],
131-
)
126+
) as PreactTable<TFeatures, TData, TSelected>
132127
}

packages/react-table/src/useTable.ts

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use client'
22

3-
import { useEffect, useLayoutEffect, useMemo, useState } from 'react'
3+
import { useMemo, useState } from 'react'
44
import { constructTable } from '@tanstack/table-core'
55
import { shallow, useStore } from '@tanstack/react-store'
66
import { FlexRender } from './FlexRender'
@@ -18,9 +18,6 @@ import type { FunctionComponent, ReactNode } from 'react'
1818
import type { FlexRenderProps } from './FlexRender'
1919
import type { SubscribeProps } from './Subscribe'
2020

21-
const useIsomorphicLayoutEffect =
22-
typeof window !== 'undefined' ? useLayoutEffect : useEffect
23-
2421
export type ReactTable<
2522
TFeatures extends TableFeatures,
2623
TData extends RowData,
@@ -73,7 +70,10 @@ export type ReactTable<
7370
*
7471
* console.log(table.state.globalFilter)
7572
*/
76-
readonly state: Readonly<TSelected>
73+
readonly state: Readonly<TSelected> & {
74+
columns: TableOptions<TFeatures, TData>['columns']
75+
data: TableOptions<TFeatures, TData>['data']
76+
}
7777
}
7878

7979
export function useTable<
@@ -109,26 +109,19 @@ export function useTable<
109109
...tableOptions,
110110
}))
111111

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-
])
112+
const selectorWithDataAndColumns = (state: TableState<TFeatures>) => ({
113+
columns: tableOptions.columns,
114+
data: tableOptions.data,
115+
...selector(state),
116+
})
124117

125-
const state = useStore(table.store, selector, shallow)
118+
const state = useStore(table.store, selectorWithDataAndColumns, shallow)
126119

127120
return useMemo(
128121
() => ({
129122
...table,
130123
state,
131124
}),
132125
[state, table],
133-
)
126+
) as ReactTable<TFeatures, TData, TSelected>
134127
}

0 commit comments

Comments
 (0)