diff --git a/src/Body/MeasureCell.tsx b/src/Body/MeasureCell.tsx index 1c938687f..b3015c5f2 100644 --- a/src/Body/MeasureCell.tsx +++ b/src/Body/MeasureCell.tsx @@ -1,5 +1,6 @@ import * as React from 'react'; import ResizeObserver from '@rc-component/resize-observer'; +import useLayoutEffect from '@rc-component/util/lib/hooks/useLayoutEffect'; export interface MeasureCellProps { columnKey: React.Key; @@ -9,7 +10,7 @@ export interface MeasureCellProps { export default function MeasureCell({ columnKey, onColumnResize }: MeasureCellProps) { const cellRef = React.useRef(); - React.useEffect(() => { + useLayoutEffect(() => { if (cellRef.current) { onColumnResize(columnKey, cellRef.current.offsetWidth); } diff --git a/src/Body/MeasureRow.tsx b/src/Body/MeasureRow.tsx index 0c1d03114..d5b38b0e0 100644 --- a/src/Body/MeasureRow.tsx +++ b/src/Body/MeasureRow.tsx @@ -1,6 +1,7 @@ import * as React from 'react'; import ResizeObserver from '@rc-component/resize-observer'; import MeasureCell from './MeasureCell'; +import isVisible from '@rc-component/util/lib/Dom/isVisible'; export interface MeasureCellProps { prefixCls: string; @@ -9,17 +10,22 @@ export interface MeasureCellProps { } export default function MeasureRow({ prefixCls, columnsKey, onColumnResize }: MeasureCellProps) { + const ref = React.useRef(null); + return ( { - infoList.forEach(({ data: columnKey, size }) => { - onColumnResize(columnKey, size.offsetWidth); - }); + if (isVisible(ref.current)) { + infoList.forEach(({ data: columnKey, size }) => { + onColumnResize(columnKey, size.offsetWidth); + }); + } }} > {columnsKey.map(columnKey => ( diff --git a/src/Table.tsx b/src/Table.tsx index c617ba494..299a48b35 100644 --- a/src/Table.tsx +++ b/src/Table.tsx @@ -27,7 +27,6 @@ import type { CompareProps } from '@rc-component/context/lib/Immutable'; import classNames from 'classnames'; import ResizeObserver from '@rc-component/resize-observer'; -import isVisible from '@rc-component/util/lib/Dom/isVisible'; import { getTargetScrollBarSize } from '@rc-component/util/lib/getScrollBarSize'; import useEvent from '@rc-component/util/lib/hooks/useEvent'; import pickAttrs from '@rc-component/util/lib/pickAttrs'; @@ -47,7 +46,7 @@ import Header from './Header/Header'; import useColumns from './hooks/useColumns'; import useExpand from './hooks/useExpand'; import useFixedInfo from './hooks/useFixedInfo'; -import { useLayoutState, useTimeoutLock } from './hooks/useFrame'; +import { useTimeoutLock } from './hooks/useFrame'; import useHover from './hooks/useHover'; import useSticky from './hooks/useSticky'; import useStickyOffsets from './hooks/useStickyOffsets'; @@ -76,6 +75,7 @@ import ColumnGroup from './sugar/ColumnGroup'; import { getColumnsKey, validateValue, validNumberValue } from './utils/valueUtil'; import { getDOM } from '@rc-component/util/lib/Dom/findDOMNode'; import isEqual from '@rc-component/util/lib/isEqual'; +import useLayoutEffect from '@rc-component/util/lib/hooks/useLayoutEffect'; export const DEFAULT_PREFIX = 'rc-table'; @@ -349,7 +349,7 @@ function Table( const scrollSummaryRef = React.useRef(); const [shadowStart, setShadowStart] = React.useState(false); const [shadowEnd, setShadowEnd] = React.useState(false); - const [colsWidths, updateColsWidths] = useLayoutState(new Map()); + const [colsWidths, updateColsWidths] = React.useState(new Map()); // Convert map to number width const colsKeys = getColumnsKey(flattenColumns); @@ -403,16 +403,14 @@ function Table( } const onColumnResize = React.useCallback((columnKey: React.Key, width: number) => { - if (isVisible(fullTableRef.current)) { - updateColsWidths(widths => { - if (widths.get(columnKey) !== width) { - const newWidths = new Map(widths); - newWidths.set(columnKey, width); - return newWidths; - } - return widths; - }); - } + updateColsWidths(widths => { + if (widths.get(columnKey) !== width) { + const newWidths = new Map(widths); + newWidths.set(columnKey, width); + return newWidths; + } + return widths; + }); }, []); const [setScrollTarget, getScrollTarget] = useTimeoutLock(null); @@ -526,7 +524,7 @@ function Table( // ===================== Effects ====================== const [scrollbarSize, setScrollbarSize] = React.useState(0); - React.useEffect(() => { + useLayoutEffect(() => { if (!tailor || !useInternalHooks) { if (scrollBodyRef.current instanceof Element) { setScrollbarSize(getTargetScrollBarSize(scrollBodyRef.current).width); diff --git a/tests/__snapshots__/FixedColumn.spec.tsx.snap b/tests/__snapshots__/FixedColumn.spec.tsx.snap index 30a4bb6bc..4e7b289b4 100644 --- a/tests/__snapshots__/FixedColumn.spec.tsx.snap +++ b/tests/__snapshots__/FixedColumn.spec.tsx.snap @@ -48,7 +48,7 @@ exports[`Table.FixedColumn > fixed column renders correctly RTL 1`] = ` fixed column renders correctly RTL 1`] = ` fixed column renders correctly RTL 1`] = ` fixed column renders correctly RTL 1`] = ` fixed column renders correctly RTL 1`] = ` fixed column renders correctly RTL 1`] = ` fixed column renders correctly RTL 1`] = ` fixed column renders correctly RTL 1`] = ` fixed column renders correctly RTL 1`] = ` fixed column renders correctly RTL 1`] = ` custom components > renders fixed column and header corre style="overflow: hidden;" > - + + + + + +