Skip to content

Commit f5f789d

Browse files
committed
check hooks rules with eslint + fix missing dependencies
note that we need to use useCallback for setColumnWidth and setColumnWidths, because otherwise the functions are recreated on every render, which reruns the useEffects inside TableHeader.tsx
1 parent 9bd138a commit f5f789d

File tree

4 files changed

+20
-8
lines changed

4 files changed

+20
-8
lines changed

eslint.config.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import javascript from '@eslint/js'
22
import typescriptParser from '@typescript-eslint/parser'
33
import react from 'eslint-plugin-react'
4+
import reactHooks from 'eslint-plugin-react-hooks'
45
import globals from 'globals'
56
import typescript from 'typescript-eslint'
67

@@ -11,7 +12,8 @@ export default [
1112
{
1213
files: ['**/*.ts', '**/*.tsx'],
1314
plugins: {
14-
react,
15+
react,
16+
'react-hooks': reactHooks,
1517
typescript,
1618
},
1719

@@ -38,6 +40,7 @@ export default [
3840
rules: {
3941
...javascript.configs.recommended.rules,
4042
...react.configs.recommended.rules,
43+
...reactHooks.configs.recommended.rules,
4144
...typescript.configs.eslintRecommended.rules,
4245
...typescript.configs.recommended.rules,
4346
'arrow-spacing': 'error',

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@
4646
"@types/react-dom": "18.3.1",
4747
"@vitest/coverage-v8": "2.1.8",
4848
"eslint": "9.18.0",
49-
"eslint-plugin-react": "7.37.3",
49+
"eslint-plugin-react": "7.37.4",
50+
"eslint-plugin-react-hooks": "5.1.0",
5051
"jsdom": "26.0.0",
5152
"tslib": "2.8.1",
5253
"typescript": "5.7.3",

src/HighTable.tsx

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,15 @@ export default function HighTable({
336336
// did not throw: we can set the anchor
337337
dispatch({ type: 'SET_SELECTION', selection: newSelection, anchor: tableIndex })
338338
}
339-
}, [selection, anchor])
339+
}, [selection, anchor, selectable])
340+
341+
const setColumnWidths = useCallback((columnWidths: Array<number | undefined>) => {
342+
dispatch({ type: 'SET_COLUMN_WIDTHS', columnWidths })
343+
}, [])
344+
345+
const setColumnWidth = useCallback((columnIndex: number, columnWidth: number | undefined) => {
346+
dispatch({ type: 'SET_COLUMN_WIDTH', columnIndex, columnWidth })
347+
}, [])
340348

341349
// add empty pre and post rows to fill the viewport
342350
const prePadding = Array.from({ length: Math.min(padding, startIndex) }, () => [])
@@ -369,8 +377,8 @@ export default function HighTable({
369377
dataReady={hasCompleteRow}
370378
header={data.header}
371379
orderBy={orderBy}
372-
setColumnWidth={(columnIndex, columnWidth) => dispatch({ type: 'SET_COLUMN_WIDTH', columnIndex, columnWidth })}
373-
setColumnWidths={columnWidths => dispatch({ type: 'SET_COLUMN_WIDTHS', columnWidths })}
380+
setColumnWidth={setColumnWidth}
381+
setColumnWidths={setColumnWidths}
374382
setOrderBy={orderBy => data.sortable && dispatch({ type: 'SET_ORDER', orderBy })} />
375383
<tbody>
376384
{prePadding.map((_, prePaddingIndex) => {

src/TableHeader.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,15 @@ export default function TableHeader({
6060
})
6161
}
6262
setColumnWidths(userWidths)
63-
}, [cacheKey])
63+
}, [cacheKey, header, setColumnWidths])
6464

6565
// Measure default column widths when data is ready
6666
useEffect(() => {
6767
if (dataReady) {
6868
const widths = headerRefs.current.map(measureWidth)
6969
setColumnWidths(widths)
7070
}
71-
}, [cacheKey, dataReady, header]) // re-measure if header changes
71+
}, [cacheKey, dataReady, header, setColumnWidths]) // re-measure if header changes
7272

7373
// Modify column width
7474
function startResizing(columnIndex: number, e: React.MouseEvent<HTMLSpanElement, MouseEvent>) {
@@ -130,7 +130,7 @@ export default function TableHeader({
130130
window.removeEventListener('mousemove', handleMouseMove)
131131
window.removeEventListener('mouseup', stopResizing)
132132
}
133-
}, [cacheKey, header, resizing, setColumnWidths])
133+
}, [cacheKey, header, resizing, setColumnWidths, columnWidths, setColumnWidth])
134134

135135
// Function to handle click for changing orderBy
136136
function handleOrderByClick(columnHeader: string, e: React.MouseEvent) {

0 commit comments

Comments
 (0)