Skip to content

Commit

Permalink
fix: exhaustive-deps in useEffect hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
Yannick Küchlin authored and Yannick Küchlin committed Oct 14, 2022
1 parent 72997ae commit f57b9ca
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 22 deletions.
1 change: 1 addition & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module.exports = {
'plugin:@typescript-eslint/recommended',
'plugin:import/recommended',
'plugin:import/typescript',
'plugin:react-hooks/recommended',
],
parser: '@typescript-eslint/parser',
parserOptions: {
Expand Down
3 changes: 1 addition & 2 deletions docs/pages/Filters.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Stack, Table, Title, Text, createStyles, Group, SimpleGrid, Card, Divider } from '@mantine/core';
import { Card, Divider, Stack, Title } from '@mantine/core';
import { Prism } from '@mantine/prism';
import { Fragment } from 'react';
import { PropertyTable } from '../components/PropertyTable';

export default function Filters() {
Expand Down
2 changes: 1 addition & 1 deletion docs/pages/examples/AsyncExample.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export default function AsyncExample() {

useEffect(() => {
load({ pageIndex: 0, pageSize: 10 });
}, []);
});

return (
<DataGrid<Data>
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"eslint-import-resolver-typescript": "^3.5.1",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-react-hooks": "^4.6.0",
"husky": "^8.0.1",
"lint-staged": "^13.0.3",
"prettier": "^2.7.1",
Expand Down
19 changes: 15 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 15 additions & 13 deletions src/DataGrid.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { RefCallback, useCallback, useEffect, useImperativeHandle, useState } from 'react';
import { LoadingOverlay, ScrollArea, Stack, Table as MantineTable, Text } from '@mantine/core';
import {
ColumnFiltersState,
Expand All @@ -10,22 +9,23 @@ import {
getSortedRowModel,
OnChangeFn,
PaginationState,
SortingState,
useReactTable,
RowData,
RowSelectionState,
SortingState,
Table,
useReactTable,
} from '@tanstack/react-table';
import { RefCallback, useCallback, useEffect, useImperativeHandle, useState } from 'react';
import { BoxOff } from 'tabler-icons-react';

import useStyles from './DataGrid.styles';

import { GlobalFilter, globalFilterFn } from './GlobalFilter';
import { ColumnSorter } from './ColumnSorter';
import { ColumnFilter } from './ColumnFilter';
import { ColumnSorter } from './ColumnSorter';
import { GlobalFilter, globalFilterFn } from './GlobalFilter';
import { DEFAULT_INITIAL_SIZE, Pagination } from './Pagination';
import { DataGridProps } from './types';
import { getRowSelectionColumn } from './RowSelection';
import { DataGridProps } from './types';

export function useDataGrid<TData extends RowData>(): [Table<TData> | null, RefCallback<Table<TData>>] {
const [state, setState] = useState<Table<TData> | null>(null);
Expand Down Expand Up @@ -125,15 +125,17 @@ export function DataGrid<TData extends RowData>({
});
useImperativeHandle(tableRef, () => table);

const tableSize = table.getTotalSize();

useEffect(() => {
if (noFlexLayout) {
setTableWidth(table.getTotalSize() + 'px');
setTableWidth(tableSize + 'px');
} else if (width) {
setTableWidth(width + 'px');
} else {
setTableWidth('100%');
}
}, [width, noFlexLayout, table.getTotalSize()]);
}, [table, width, noFlexLayout, tableSize]);

const handleGlobalFilterChange: OnChangeFn<string> = useCallback(
(arg0) =>
Expand All @@ -145,7 +147,7 @@ export function DataGrid<TData extends RowData>({
globalFilter: next,
};
}),
[onSearch]
[table, onSearch]
);

const handleSortingChange: OnChangeFn<SortingState> = useCallback(
Expand All @@ -158,7 +160,7 @@ export function DataGrid<TData extends RowData>({
sorting: next,
};
}),
[onSort]
[table, onSort]
);

const handleColumnFiltersChange: OnChangeFn<ColumnFiltersState> = useCallback(
Expand All @@ -171,7 +173,7 @@ export function DataGrid<TData extends RowData>({
columnFilters: next,
};
}),
[onFilter]
[table, onFilter]
);

const handlePaginationChange: OnChangeFn<PaginationState> = useCallback(
Expand All @@ -186,7 +188,7 @@ export function DataGrid<TData extends RowData>({
}));
}
},
[onPageChange]
[table, onPageChange]
);

const handleRowSelectionChange: OnChangeFn<RowSelectionState> = useCallback(
Expand All @@ -200,7 +202,7 @@ export function DataGrid<TData extends RowData>({
};
});
},
[onRowSelectionChange]
[table, onRowSelectionChange]
);

const pageCount = withPagination && total ? Math.ceil(total / table.getState().pagination.pageSize) : undefined;
Expand Down
2 changes: 1 addition & 1 deletion src/GlobalFilter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export function GlobalFilter<TData>({ table, className, locale }: GlobalFilterPr
}, 200);

return () => clearTimeout(timeout);
}, [value]);
}, [table, value]);

return (
<TextInput
Expand Down
2 changes: 1 addition & 1 deletion src/Pagination.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Group, Select, Text, Pagination as MantinePagination, Box, MantineNumberSize } from '@mantine/core';
import { Box, Group, MantineNumberSize, Pagination as MantinePagination, Select, Text } from '@mantine/core';
import { Table } from '@tanstack/react-table';
import { DataGridLocale, PaginationMode } from './types';

Expand Down

0 comments on commit f57b9ca

Please sign in to comment.