diff --git a/src/Body/BodyRow.tsx b/src/Body/BodyRow.tsx index baf6f9e5f..928d3b808 100644 --- a/src/Body/BodyRow.tsx +++ b/src/Body/BodyRow.tsx @@ -3,9 +3,9 @@ import * as React from 'react'; import Cell from '../Cell'; import { responseImmutable } from '../context/TableContext'; import devRenderTimes from '../hooks/useRenderTimes'; +import useRowInfo from '../hooks/useRowInfo'; import type { ColumnType, CustomizeComponent, GetRowKey } from '../interface'; import ExpandedRow from './ExpandedRow'; -import useRowInfo from '../hooks/useRowInfo'; export interface BodyRowProps { record: RecordType; @@ -116,18 +116,14 @@ function BodyRow( rowSupportExpand, } = rowInfo; - const [expandRended, setExpandRended] = React.useState(false); + // Force render expand row if expanded before + const expandedRef = React.useRef(false); + expandedRef.current ||= expanded; if (process.env.NODE_ENV !== 'production') { devRenderTimes(props); } - React.useEffect(() => { - if (expanded) { - setExpandRended(true); - } - }, [expanded]); - // ======================== Base tr row ======================== const baseRowNode = ( ( // ======================== Expand Row ========================= let expandRowNode: React.ReactElement; - if (rowSupportExpand && (expandRended || expanded)) { + if (rowSupportExpand && (expandedRef.current || expanded)) { const expandContent = expandedRowRender(record, index, indent + 1, expanded); const computedExpandedRowClassName = expandedRowClassName && expandedRowClassName(record, index, indent); diff --git a/tests/ExpandRow.spec.jsx b/tests/ExpandRow.spec.jsx index 0fd701f7f..3938ea1f5 100644 --- a/tests/ExpandRow.spec.jsx +++ b/tests/ExpandRow.spec.jsx @@ -1,3 +1,4 @@ +import { render } from '@testing-library/react'; import { mount } from 'enzyme'; import { spyElementPrototype } from 'rc-util/lib/test/domHook'; import { resetWarned } from 'rc-util/lib/warning'; @@ -636,4 +637,18 @@ describe('Table.Expand', () => { ); errorSpy.mockRestore(); }); + + it('should only trigger once', () => { + const expandedRowRender = vi.fn(() =>

extra data

); + render( + createTable({ + expandable: { + expandedRowRender, + expandedRowKeys: [0], + }, + }), + ); + + expect(expandedRowRender).toHaveBeenCalledTimes(1); + }); });