Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed an issue with simple memo components being updated with new props during context change #14876

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion packages/react-reconciler/src/ReactFiberBeginWork.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -592,8 +592,13 @@ function updateSimpleMemoComponent(
}
if (current !== null) {
const prevProps = current.memoizedProps;
// 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 (
shallowEqual(prevProps, nextProps) &&
prevProps === nextProps &&
current.ref === workInProgress.ref &&
// Prevent bailout if the implementation changed due to hot reload.
(__DEV__ ? workInProgress.type === current.type : true)
Expand Down
7 changes: 6 additions & 1 deletion packages/react-reconciler/src/ReactFiberBeginWork.old.js
Original file line number Diff line number Diff line change
Expand Up @@ -592,8 +592,13 @@ function updateSimpleMemoComponent(
}
if (current !== null) {
const prevProps = current.memoizedProps;
// 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 (
shallowEqual(prevProps, nextProps) &&
prevProps === nextProps &&
current.ref === workInProgress.ref &&
// Prevent bailout if the implementation changed due to hot reload.
(__DEV__ ? workInProgress.type === current.type : true)
Expand Down
51 changes: 51 additions & 0 deletions packages/react-reconciler/src/__tests__/ReactMemo-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,57 @@ describe('memo', () => {
expect(ReactNoop.getChildren()).toEqual([span('Count: 1')]);
});

it('bails out on props referential equality during context change', async () => {
const CountContext = React.createContext(0);
let renderCount = 0;

function Inner(props) {
React.useContext(CountContext);
return React.useMemo(() => {
const text = `Inner render count: ${++renderCount}`;
return <Text text={text} />;
}, [props]);
}

Inner = memo(Inner);

function Outer(props) {
React.useContext(CountContext);
return <Inner />;
}

function Parent({value}) {
return (
<Suspense fallback={<Text text="Loading..." />}>
<CountContext.Provider value={value}>
<Outer />
</CountContext.Provider>
</Suspense>
);
}

let ctxValue = 0;
ReactNoop.render(<Parent value={ctxValue++} />);
expect(Scheduler).toFlushAndYield(['Loading...']);
await Promise.resolve();
expect(Scheduler).toFlushAndYield(['Inner render count: 1']);
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'),
]);

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

it('accepts custom comparison function', async () => {
function Counter({count}) {
return <Text text={count} />;
Expand Down