Skip to content

Commit 0c52e24

Browse files
Brian Vaughnhanq08
Brian Vaughn
andauthored
Support inner component _debugOwner in memo (#19556)
* Support inner component _debugOwner in memo * test with devtool context * remove memo test * Merged master; tweaked test and snapshot * Pass owner to createFiber fn when creating a memo component. Co-authored-by: Theodore Han <tqhan317@gmail.com>
1 parent 94c0244 commit 0c52e24

File tree

6 files changed

+76
-2
lines changed

6 files changed

+76
-2
lines changed

packages/react-devtools-shared/src/__tests__/__snapshots__/ownersListContext-test.js.snap

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,33 @@ Array [
7878
]
7979
`;
8080
81+
exports[`OwnersListContext should include all owners for a component wrapped in react memo: owners for "InnerComponent" 1`] = `
82+
Array [
83+
Object {
84+
"displayName": "Grandparent",
85+
"hocDisplayNames": null,
86+
"id": 2,
87+
"type": 5,
88+
},
89+
Object {
90+
"displayName": "InnerComponent",
91+
"hocDisplayNames": Array [
92+
"Memo",
93+
],
94+
"id": 3,
95+
"type": 8,
96+
},
97+
Object {
98+
"displayName": "InnerComponent",
99+
"hocDisplayNames": Array [
100+
"ForwardRef",
101+
],
102+
"id": 4,
103+
"type": 6,
104+
},
105+
]
106+
`;
107+
81108
exports[`OwnersListContext should include the current element even if there are no other owners: mount 1`] = `
82109
[root]
83110
<Grandparent>

packages/react-devtools-shared/src/__tests__/ownersListContext-test.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,4 +206,43 @@ describe('OwnersListContext', () => {
206206

207207
done();
208208
});
209+
210+
it('should include all owners for a component wrapped in react memo', async done => {
211+
const InnerComponent = (props, ref) => <div ref={ref} />;
212+
const ForwardRef = React.forwardRef(InnerComponent);
213+
const Memo = React.memo(ForwardRef);
214+
const Grandparent = () => {
215+
const ref = React.createRef();
216+
return <Memo ref={ref} />;
217+
};
218+
219+
utils.act(() =>
220+
ReactDOM.render(<Grandparent />, document.createElement('div')),
221+
);
222+
223+
let didFinish = false;
224+
function Suspender({owner}) {
225+
const read = React.useContext(OwnersListContext);
226+
const owners = read(owner.id);
227+
didFinish = true;
228+
expect(owners.length).toBe(3);
229+
expect(owners).toMatchSnapshot(
230+
`owners for "${(owner && owner.displayName) || ''}"`,
231+
);
232+
return null;
233+
}
234+
235+
const wrapped = ((store.getElementAtIndex(2): any): Element);
236+
await utils.actAsync(() =>
237+
TestRenderer.create(
238+
<Contexts defaultOwnerID={wrapped.id}>
239+
<React.Suspense fallback={null}>
240+
<Suspender owner={wrapped} />
241+
</React.Suspense>
242+
</Contexts>,
243+
),
244+
);
245+
expect(didFinish).toBe(true);
246+
done();
247+
});
209248
});

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,10 @@ export function createFiberFromTypeAndProps(
579579
fiber.type = resolvedType;
580580
fiber.lanes = lanes;
581581

582+
if (__DEV__) {
583+
fiber._debugOwner = owner;
584+
}
585+
582586
return fiber;
583587
}
584588

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,10 @@ export function createFiberFromTypeAndProps(
573573
fiber.type = resolvedType;
574574
fiber.lanes = lanes;
575575

576+
if (__DEV__) {
577+
fiber._debugOwner = owner;
578+
}
579+
576580
return fiber;
577581
}
578582

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ function updateMemoComponent(
434434
Component.type,
435435
null,
436436
nextProps,
437-
null,
437+
workInProgress,
438438
workInProgress.mode,
439439
renderLanes,
440440
);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ function updateMemoComponent(
434434
Component.type,
435435
null,
436436
nextProps,
437-
null,
437+
workInProgress,
438438
workInProgress.mode,
439439
renderLanes,
440440
);

0 commit comments

Comments
 (0)