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 bd13426
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
6 changes: 5 additions & 1 deletion packages/react-reconciler/src/ReactFiberBeginWork.new.js
Expand Up @@ -592,7 +592,11 @@ function updateSimpleMemoComponent(
}
if (current !== null) {
const prevProps = current.memoizedProps;
nextProps = shallowEqual(prevProps, nextProps) ? prevProps : nextProps;
// potentially recycle `prevProps` if they are the same
// this allows hooks depending on the `props` to be reused
workInProgress.pendingProps = nextProps = shallowEqual(prevProps, nextProps)
? prevProps
: nextProps;
if (
prevProps === nextProps &&
current.ref === workInProgress.ref &&
Expand Down
6 changes: 5 additions & 1 deletion packages/react-reconciler/src/ReactFiberBeginWork.old.js
Expand Up @@ -592,7 +592,11 @@ function updateSimpleMemoComponent(
}
if (current !== null) {
const prevProps = current.memoizedProps;
nextProps = shallowEqual(prevProps, nextProps) ? prevProps : nextProps;
// potentially recycle `prevProps` if they are the same
// this allows hooks depending on the `props` to be reused
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
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 bd13426

Please sign in to comment.