Skip to content

Commit 6e49703

Browse files
authored
enhance: Rm duplicated re-render of expanded row (#1022)
* test: test driven * fix: not expanded for rerender
1 parent f6e1871 commit 6e49703

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

src/Body/BodyRow.tsx

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import * as React from 'react';
33
import Cell from '../Cell';
44
import { responseImmutable } from '../context/TableContext';
55
import devRenderTimes from '../hooks/useRenderTimes';
6+
import useRowInfo from '../hooks/useRowInfo';
67
import type { ColumnType, CustomizeComponent, GetRowKey } from '../interface';
78
import ExpandedRow from './ExpandedRow';
8-
import useRowInfo from '../hooks/useRowInfo';
99

1010
export interface BodyRowProps<RecordType> {
1111
record: RecordType;
@@ -116,18 +116,14 @@ function BodyRow<RecordType extends { children?: readonly RecordType[] }>(
116116
rowSupportExpand,
117117
} = rowInfo;
118118

119-
const [expandRended, setExpandRended] = React.useState(false);
119+
// Force render expand row if expanded before
120+
const expandedRef = React.useRef(false);
121+
expandedRef.current ||= expanded;
120122

121123
if (process.env.NODE_ENV !== 'production') {
122124
devRenderTimes(props);
123125
}
124126

125-
React.useEffect(() => {
126-
if (expanded) {
127-
setExpandRended(true);
128-
}
129-
}, [expanded]);
130-
131127
// ======================== Base tr row ========================
132128
const baseRowNode = (
133129
<RowComponent
@@ -181,7 +177,7 @@ function BodyRow<RecordType extends { children?: readonly RecordType[] }>(
181177

182178
// ======================== Expand Row =========================
183179
let expandRowNode: React.ReactElement;
184-
if (rowSupportExpand && (expandRended || expanded)) {
180+
if (rowSupportExpand && (expandedRef.current || expanded)) {
185181
const expandContent = expandedRowRender(record, index, indent + 1, expanded);
186182
const computedExpandedRowClassName =
187183
expandedRowClassName && expandedRowClassName(record, index, indent);

tests/ExpandRow.spec.jsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { render } from '@testing-library/react';
12
import { mount } from 'enzyme';
23
import { spyElementPrototype } from 'rc-util/lib/test/domHook';
34
import { resetWarned } from 'rc-util/lib/warning';
@@ -636,4 +637,18 @@ describe('Table.Expand', () => {
636637
);
637638
errorSpy.mockRestore();
638639
});
640+
641+
it('should only trigger once', () => {
642+
const expandedRowRender = vi.fn(() => <p>extra data</p>);
643+
render(
644+
createTable({
645+
expandable: {
646+
expandedRowRender,
647+
expandedRowKeys: [0],
648+
},
649+
}),
650+
);
651+
652+
expect(expandedRowRender).toHaveBeenCalledTimes(1);
653+
});
639654
});

0 commit comments

Comments
 (0)