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

fix[devtools]: check if fiber is unmounted before trying to highlight #26983

Merged
Merged
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
5 changes: 5 additions & 0 deletions packages/react-devtools-shared/src/backend/legacy/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1101,6 +1101,10 @@ export function attach(

function unpatchConsoleForStrictMode() {}

function hasFiberWithId(id: number): boolean {
return idToInternalInstanceMap.has(id);
}

return {
clearErrorsAndWarnings,
clearErrorsForFiberID,
Expand All @@ -1124,6 +1128,7 @@ export function attach(
handleCommitFiberRoot,
handleCommitFiberUnmount,
handlePostCommitFiberRoot,
hasFiberWithId,
inspectElement,
logElementToConsole,
overrideError,
Expand Down
23 changes: 9 additions & 14 deletions packages/react-devtools-shared/src/backend/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2736,21 +2736,11 @@ export function attach(

function findNativeNodesForFiberID(id: number) {
try {
let fiber = findCurrentFiberUsingSlowPathById(id);
const fiber = findCurrentFiberUsingSlowPathById(id);
if (fiber === null) {
return null;
}
// Special case for a timed-out Suspense.
const isTimedOutSuspense =
fiber.tag === SuspenseComponent && fiber.memoizedState !== null;
if (isTimedOutSuspense) {
// A timed-out Suspense's findDOMNode is useless.
// Try our best to find the fallback directly.
const maybeFallbackFiber = fiber.child && fiber.child.sibling;
if (maybeFallbackFiber != null) {
fiber = maybeFallbackFiber;
}
}
Comment on lines -2743 to -2753
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not needed, because we return hostFibers and this branch doesn't have any side effects

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice find!


const hostFibers = findAllCurrentHostFibers(id);
return hostFibers.map(hostFiber => hostFiber.stateNode).filter(Boolean);
} catch (err) {
Expand All @@ -2759,9 +2749,9 @@ export function attach(
}
}

function getDisplayNameForFiberID(id: number) {
function getDisplayNameForFiberID(id: number): null | string {
const fiber = idToArbitraryFiberMap.get(id);
return fiber != null ? getDisplayNameForFiber(((fiber: any): Fiber)) : null;
return fiber != null ? getDisplayNameForFiber(fiber) : null;
}

function getFiberForNative(hostInstance: NativeType) {
Expand Down Expand Up @@ -4456,6 +4446,10 @@ export function attach(
traceUpdatesEnabled = isEnabled;
}

function hasFiberWithId(id: number): boolean {
return idToArbitraryFiberMap.has(id);
}

return {
cleanup,
clearErrorsAndWarnings,
Expand All @@ -4476,6 +4470,7 @@ export function attach(
handleCommitFiberRoot,
handleCommitFiberUnmount,
handlePostCommitFiberRoot,
hasFiberWithId,
inspectElement,
logElementToConsole,
patchConsoleForStrictMode,
Expand Down
1 change: 1 addition & 0 deletions packages/react-devtools-shared/src/backend/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ export type RendererInterface = {
handleCommitFiberRoot: (fiber: Object, commitPriority?: number) => void,
handleCommitFiberUnmount: (fiber: Object) => void,
handlePostCommitFiberRoot: (fiber: Object) => void,
hasFiberWithId: (id: number) => boolean,
inspectElement: (
requestID: number,
id: number,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,21 @@ export default function setupHighlighter(
const renderer = agent.rendererInterfaces[rendererID];
if (renderer == null) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why == and not ===?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To check if a value isn't equal to undefined or null with a single statement

console.warn(`Invalid renderer id "${rendererID}" for element "${id}"`);

hideOverlay(agent);
return;
}

let nodes: ?Array<HTMLElement> = null;
if (renderer != null) {
nodes = ((renderer.findNativeNodesForFiberID(
id,
): any): ?Array<HTMLElement>);
// In some cases fiber may already be unmounted
if (!renderer.hasFiberWithId(id)) {
hideOverlay(agent);
return;
}

const nodes: ?Array<HTMLElement> = (renderer.findNativeNodesForFiberID(
id,
): any);

if (nodes != null && nodes[0] != null) {
const node = nodes[0];
// $FlowFixMe[method-unbinding]
Expand Down