Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ describe('Profiler change descriptions', () => {
▾ <App>
▾ <Context.Provider>
<Child>
▾ <Child> [Memo]
<Child>
<Child> [Memo]
▾ <RefForwardingComponent> [ForwardRef]
<Child>
`);
Expand Down
7 changes: 7 additions & 0 deletions packages/react-devtools-shared/src/__tests__/store-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1887,6 +1887,11 @@ describe('Store', () => {
ForwardRefComponentWithCustomDisplayName.displayName = 'Custom';
const MyComponent4 = (props, ref) => null;
const MemoComponent = React.memo(MyComponent4);
const MyComponent5 = (props, ref) => null;
const MemoComponentWithCustomCompare = React.memo(
MyComponent5,
(a, b) => a === b,
);
const MemoForwardRefComponent = React.memo(ForwardRefComponent);

const FakeHigherOrderComponent = () => null;
Expand Down Expand Up @@ -1916,6 +1921,7 @@ describe('Store', () => {
<ForwardRefComponentWithAnonymousFunction />
<ForwardRefComponentWithCustomDisplayName />
<MemoComponent />
<MemoComponentWithCustomCompare />
<MemoForwardRefComponent />
<FakeHigherOrderComponent />
<MemoizedFakeHigherOrderComponent />
Expand All @@ -1942,6 +1948,7 @@ describe('Store', () => {
<MyComponent2>
<Custom>
<MyComponent4> [Memo]
<MyComponent5> [Memo]
▾ <MyComponent> [Memo]
<MyComponent> [ForwardRef]
<Baz> [withFoo][withBar]
Expand Down
17 changes: 13 additions & 4 deletions packages/react-reconciler/src/ReactFiberBeginWork.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,12 @@ export const SelectiveHydrationException: mixed = new Error(

let didReceiveUpdate: boolean = false;

// Map from SimpleMemoComponent function to compare a.k.a. arePropsEqual function.
const simpleMemoComponentToCompare = new WeakMap<
Function,
(objA: mixed, objB: mixed) => boolean,
>();

let didWarnAboutBadClass;
let didWarnAboutContextTypeOnFunctionComponent;
let didWarnAboutContextTypes;
Expand Down Expand Up @@ -478,16 +484,18 @@ function updateMemoComponent(
): null | Fiber {
if (current === null) {
const type = Component.type;
if (isSimpleFunctionComponent(type) && Component.compare === null) {
if (isSimpleFunctionComponent(type)) {
let resolvedType = type;
if (__DEV__) {
resolvedType = resolveFunctionForHotReloading(type);
}
// If this is a plain function component without default props,
// and with only the default shallow comparison, we upgrade it
// to a SimpleMemoComponent to allow fast path updates.
// we upgrade it to a SimpleMemoComponent to allow fast path updates.
workInProgress.tag = SimpleMemoComponent;
workInProgress.type = resolvedType;
if (Component.compare) {
simpleMemoComponentToCompare.set(resolvedType, Component.compare);
}
if (__DEV__) {
validateFunctionComponentInDev(workInProgress, type);
}
Expand Down Expand Up @@ -548,9 +556,10 @@ function updateSimpleMemoComponent(
// hasn't yet mounted. This happens when the inner render suspends.
// We'll need to figure out if this is fine or can cause issues.
if (current !== null) {
const compare = simpleMemoComponentToCompare.get(Component) ?? shallowEqual;
const prevProps = current.memoizedProps;
if (
shallowEqual(prevProps, nextProps) &&
compare(prevProps, nextProps) &&
current.ref === workInProgress.ref &&
// Prevent bailout if the implementation changed due to hot reload.
(__DEV__ ? workInProgress.type === current.type : true)
Expand Down
Loading