Skip to content

Commit bcc7974

Browse files
committed
fix(table-filter): use ref to stabilize handleRemove/handleApply callbacks
Reading rules via ref instead of closure eliminates rules from useCallback dependency arrays, keeping callbacks stable across rule edits and preserving the memo() benefit on FilterRuleRow.
1 parent e622b6e commit bcc7974

File tree

1 file changed

+10
-5
lines changed
  • apps/sim/app/workspace/[workspaceId]/tables/[tableId]/components/table-filter

1 file changed

+10
-5
lines changed

apps/sim/app/workspace/[workspaceId]/tables/[tableId]/components/table-filter/table-filter.tsx

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

3-
import { memo, useCallback, useMemo, useState } from 'react'
3+
import { memo, useCallback, useMemo, useRef, useState } from 'react'
44
import { X } from 'lucide-react'
55
import { nanoid } from 'nanoid'
66
import {
@@ -32,6 +32,11 @@ export function TableFilter({ columns, filter, onApply, onClose }: TableFilterPr
3232
return fromFilter.length > 0 ? fromFilter : [createRule(columns)]
3333
})
3434

35+
// Ref kept in sync each render so callbacks can read current rules
36+
// without capturing them in their dependency arrays (keeps memo stable)
37+
const rulesRef = useRef(rules)
38+
rulesRef.current = rules
39+
3540
const columnOptions = useMemo(
3641
() => columns.map((col) => ({ value: col.name, label: col.name })),
3742
[columns]
@@ -43,7 +48,7 @@ export function TableFilter({ columns, filter, onApply, onClose }: TableFilterPr
4348

4449
const handleRemove = useCallback(
4550
(id: string) => {
46-
const next = rules.filter((r) => r.id !== id)
51+
const next = rulesRef.current.filter((r) => r.id !== id)
4752
if (next.length === 0) {
4853
onApply(null)
4954
onClose()
@@ -52,7 +57,7 @@ export function TableFilter({ columns, filter, onApply, onClose }: TableFilterPr
5257
setRules(next)
5358
}
5459
},
55-
[columns, onApply, onClose, rules]
60+
[columns, onApply, onClose]
5661
)
5762

5863
const handleUpdate = useCallback((id: string, field: keyof FilterRule, value: string) => {
@@ -68,9 +73,9 @@ export function TableFilter({ columns, filter, onApply, onClose }: TableFilterPr
6873
}, [])
6974

7075
const handleApply = useCallback(() => {
71-
const validRules = rules.filter((r) => r.column && r.value)
76+
const validRules = rulesRef.current.filter((r) => r.column && r.value)
7277
onApply(filterRulesToFilter(validRules))
73-
}, [rules, onApply])
78+
}, [onApply])
7479

7580
const handleClear = useCallback(() => {
7681
setRules([createRule(columns)])

0 commit comments

Comments
 (0)