Skip to content

Commit bd13426

Browse files
committed
Recycle prevProps in simple memo based on the shallowEqual check
1 parent 4607937 commit bd13426

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

packages/react-reconciler/src/ReactFiberBeginWork.new.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,11 @@ function updateSimpleMemoComponent(
592592
}
593593
if (current !== null) {
594594
const prevProps = current.memoizedProps;
595-
nextProps = shallowEqual(prevProps, nextProps) ? prevProps : nextProps;
595+
// potentially recycle `prevProps` if they are the same
596+
// this allows hooks depending on the `props` to be reused
597+
workInProgress.pendingProps = nextProps = shallowEqual(prevProps, nextProps)
598+
? prevProps
599+
: nextProps;
596600
if (
597601
prevProps === nextProps &&
598602
current.ref === workInProgress.ref &&

packages/react-reconciler/src/ReactFiberBeginWork.old.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,11 @@ function updateSimpleMemoComponent(
592592
}
593593
if (current !== null) {
594594
const prevProps = current.memoizedProps;
595-
nextProps = shallowEqual(prevProps, nextProps) ? prevProps : nextProps;
595+
// potentially recycle `prevProps` if they are the same
596+
// this allows hooks depending on the `props` to be reused
597+
workInProgress.pendingProps = nextProps = shallowEqual(prevProps, nextProps)
598+
? prevProps
599+
: nextProps;
596600
if (
597601
prevProps === nextProps &&
598602
current.ref === workInProgress.ref &&

packages/react-reconciler/src/__tests__/ReactMemo-test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,12 @@ describe('memo', () => {
222222
expect(ReactNoop.getChildren()).toEqual([
223223
span('Inner render count: 1'),
224224
]);
225+
226+
ReactNoop.render(<Parent value={ctxValue++} />);
227+
expect(Scheduler).toFlushAndYield([]);
228+
expect(ReactNoop.getChildren()).toEqual([
229+
span('Inner render count: 1'),
230+
]);
225231
});
226232

227233
it('accepts custom comparison function', async () => {

0 commit comments

Comments
 (0)