diff --git a/src/Cell/index.tsx b/src/Cell/index.tsx index 92dd4c4d9..ff90c3941 100644 --- a/src/Cell/index.tsx +++ b/src/Cell/index.tsx @@ -42,7 +42,7 @@ export interface CellProps { ellipsis?: CellEllipsisType; align?: AlignType; - shouldCellUpdate?: (record: RecordType) => boolean; + shouldCellUpdate?: (record: RecordType, prevRecord: RecordType) => boolean; // Fixed fixLeft?: number | false; @@ -207,9 +207,9 @@ function Cell( const RefCell = React.forwardRef>(Cell); RefCell.displayName = 'Cell'; -const MemoCell = React.memo(RefCell, (_, next: CellProps) => { +const MemoCell = React.memo(RefCell, (prev: CellProps, next: CellProps) => { if (next.shouldCellUpdate) { - return !next.shouldCellUpdate(next.record); + return !next.shouldCellUpdate(next.record, prev.record); } return false; diff --git a/src/interface.ts b/src/interface.ts index b8a56b2ad..dfeaeb8b8 100644 --- a/src/interface.ts +++ b/src/interface.ts @@ -81,7 +81,7 @@ export interface ColumnType extends ColumnSharedType { record: RecordType, index: number, ) => React.ReactNode | RenderedCell; - shouldCellUpdate?: (record: RecordType) => boolean; + shouldCellUpdate?: (record: RecordType, prevRecord: RecordType) => boolean; rowSpan?: number; width?: number | string; onCell?: GetComponentProps; diff --git a/tests/Table.spec.js b/tests/Table.spec.js index d9efbbbe7..dddaae792 100644 --- a/tests/Table.spec.js +++ b/tests/Table.spec.js @@ -965,18 +965,24 @@ describe('Table.Basic', () => { const record = { key: 1 }; let shouldUpdate = false; let renderTimes = 0; + let prev; + let next; - const Demo = () => { + const Demo = ({ records }) => { const [, forceUpdate] = React.useState({}); return ( <> shouldUpdate, + shouldCellUpdate: (nextRecord, prevRecord) => { + next = nextRecord; + prev = prevRecord; + return shouldUpdate; + }, render() { renderTimes += 1; return null; @@ -994,7 +1000,7 @@ describe('Table.Basic', () => { ); }; - const wrapper = mount(); + const wrapper = mount(); renderTimes = 0; wrapper.find('button').simulate('click'); @@ -1003,5 +1009,12 @@ describe('Table.Basic', () => { shouldUpdate = true; wrapper.find('button').simulate('click'); expect(renderTimes).toEqual(1); + + // Should update match prev & next + const newRecord = { ...record, next: true }; + wrapper.setProps({ records: [newRecord] }); + // wrapper.update(); + expect(prev).toBe(record); + expect(next).toBe(newRecord); }); });