Skip to content

Commit cd97cfd

Browse files
committed
fix: react and preact useTable to re-render when data and columns change
1 parent 2a4533e commit cd97cfd

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

packages/preact-table/src/useTable.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useEffect, useLayoutEffect, useMemo, useState } from 'preact/hooks'
22
import { constructTable } from '@tanstack/table-core'
3-
import { useStore } from '@tanstack/preact-store'
3+
import { shallow, useStore } from '@tanstack/preact-store'
44
import { FlexRender } from './FlexRender'
55
import { Subscribe } from './Subscribe'
66
import type {
@@ -120,7 +120,7 @@ export function useTable<
120120
table.options.state, // sync preact state to the table store
121121
])
122122

123-
const state = useStore(table.store, selector)
123+
const state = useStore(table.store, selector, { equal: shallow })
124124

125125
return useMemo(
126126
() => ({

packages/react-table/src/useTable.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,24 @@ export function useTable<
103103
return tableInstance
104104
})
105105

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+
])
113124

114125
const state = useStore(table.store, selector, shallow)
115126

0 commit comments

Comments
 (0)