Skip to content

Commit

Permalink
feat: trigger mount when window resize
Browse files Browse the repository at this point in the history
  • Loading branch information
hemengke1997 committed Jul 9, 2021
1 parent dee7f81 commit 83d2d87
Showing 1 changed file with 5 additions and 12 deletions.
17 changes: 5 additions & 12 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import React from 'react';
import { useRef } from 'react';
import ResizableHeader from './ResizableHeader';
import useDebounceFn from './utils/useDebounceFn';
import useFunction from './utils/useFunction';

function useTableResizableHeader<ColumnType extends Record<string, any>>(
Expand All @@ -13,7 +11,7 @@ function useTableResizableHeader<ColumnType extends Record<string, any>>(

const [tableWidth, setTableWidth] = React.useState<number>();

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

const onResize = useFunction((index: number) => (width: number) => {
if (width) {
Expand All @@ -36,21 +34,16 @@ function useTableResizableHeader<ColumnType extends Record<string, any>>(
width: column.width,
onMount: onResize(index),
onResize: onResize(index),
triggerMount: triggerMount.current,
triggerMount,
}),
})) as ColumnType[];
setResizableColumns(t);
triggerMount.current += 1;
}, [columns]);

const { run: onWindowResize } = useDebounceFn(() => {
triggerMount.current += 1;
});
}, [columns, triggerMount]);

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

Expand Down

0 comments on commit 83d2d87

Please sign in to comment.