Skip to content

Commit

Permalink
Recycle prevProps in simple memo based on the shallowEqual check
Browse files Browse the repository at this point in the history
  • Loading branch information
Andarist committed Apr 15, 2022
1 parent 4607937 commit 60daa37
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
4 changes: 3 additions & 1 deletion packages/react-reconciler/src/ReactFiberBeginWork.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,9 @@ function updateSimpleMemoComponent(
}
if (current !== null) {
const prevProps = current.memoizedProps;
nextProps = shallowEqual(prevProps, nextProps) ? prevProps : nextProps;
workInProgress.pendingProps = nextProps = shallowEqual(prevProps, nextProps)
? prevProps
: nextProps;
if (
prevProps === nextProps &&
current.ref === workInProgress.ref &&
Expand Down
4 changes: 3 additions & 1 deletion packages/react-reconciler/src/ReactFiberBeginWork.old.js
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,9 @@ function updateSimpleMemoComponent(
}
if (current !== null) {
const prevProps = current.memoizedProps;
nextProps = shallowEqual(prevProps, nextProps) ? prevProps : nextProps;
workInProgress.pendingProps = nextProps = shallowEqual(prevProps, nextProps)
? prevProps
: nextProps;
if (
prevProps === nextProps &&
current.ref === workInProgress.ref &&
Expand Down
6 changes: 6 additions & 0 deletions packages/react-reconciler/src/__tests__/ReactMemo-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,12 @@ describe('memo', () => {
expect(ReactNoop.getChildren()).toEqual([
span('Inner render count: 1'),
]);

ReactNoop.render(<Parent value={ctxValue++} />);
expect(Scheduler).toFlushAndYield([]);
expect(ReactNoop.getChildren()).toEqual([
span('Inner render count: 1'),
]);
});

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

0 comments on commit 60daa37

Please sign in to comment.