Skip to content

Commit

Permalink
fix: hooks render warning
Browse files Browse the repository at this point in the history
  • Loading branch information
hemengke1997 committed Jul 15, 2021
1 parent 553d7db commit b70409e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 37 deletions.
36 changes: 19 additions & 17 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React from 'react';
import ResizableHeader from './ResizableHeader';
import useFunction from './utils/useFunction';

function useTableResizableHeader<ColumnType extends Record<string, any>>(
columns: ColumnType[] | undefined,
Expand All @@ -13,26 +12,29 @@ function useTableResizableHeader<ColumnType extends Record<string, any>>(

const [triggerMount, forceRender] = React.useReducer((s) => s + 1, 0);

const onMount = useFunction((index: number) => (width: number) => {
if (width) {
setResizableColumns((t) => {
const nextColumns = [...t];
nextColumns[index] = {
...nextColumns[index],
width,
};
return nextColumns;
});
}
});
const onMount = React.useCallback(
(index: number) => (width: number) => {
if (width) {
setResizableColumns((t) => {
const nextColumns = [...t];
nextColumns[index] = {
...nextColumns[index],
width,
};
return nextColumns;
});
}
},
[],
);

const onResize = onMount;

React.useEffect(() => {
React.useLayoutEffect(() => {
forceRender();
}, [columns]);

React.useEffect(() => {
React.useLayoutEffect(() => {
const t = columns?.map((col, index) => {
const isLast = index === columns.length - 1;
return {
Expand All @@ -52,14 +54,14 @@ function useTableResizableHeader<ColumnType extends Record<string, any>>(
setResizableColumns(t);
}, [triggerMount]);

React.useEffect(() => {
React.useLayoutEffect(() => {
window.addEventListener('resize', forceRender);
return () => {
window.removeEventListener('resize', forceRender);
};
}, []);

React.useEffect(() => {
React.useLayoutEffect(() => {
const width = resizableColumns.reduce((total, current) => {
return (
total +
Expand Down
20 changes: 0 additions & 20 deletions src/utils/useFunction.ts

This file was deleted.

0 comments on commit b70409e

Please sign in to comment.