Skip to content

Commit

Permalink
Read constructor name more carefully (#29954)
Browse files Browse the repository at this point in the history
## Summary

Sometimes `constructor` happens to be the name of an unrelated property,
or we may be dealing with a `Proxy` that intercepts every read. Verify
the constructor is a function before using its name, and reset the name
anyway if it turns out not to be serializable.

Fixes some cases of the devtools crashing and becoming inoperable upon
attempting to inspect components whose props are Hookstate `State`s.

## How did you test this change?

Installed a patched version of the extension and confirmed that it
solves the problem.

---------

Co-authored-by: Ruslan Lesiutin <rdlesyutin@gmail.com>
  • Loading branch information
LoganDark and hoxyq authored Jun 25, 2024
1 parent 708d8f8 commit 133ada7
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions packages/react-devtools-shared/src/hydration.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ function createDehydrated(
preview_long: formatDataForPreview(data, true),
preview_short: formatDataForPreview(data, false),
name:
!data.constructor || data.constructor.name === 'Object'
typeof data.constructor !== 'function' ||
typeof data.constructor.name !== 'string' ||
data.constructor.name === 'Object'
? ''
: data.constructor.name,
};
Expand Down Expand Up @@ -240,7 +242,9 @@ export function dehydrate(
preview_short: formatDataForPreview(data, false),
preview_long: formatDataForPreview(data, true),
name:
!data.constructor || data.constructor.name === 'Object'
typeof data.constructor !== 'function' ||
typeof data.constructor.name !== 'string' ||
data.constructor.name === 'Object'
? ''
: data.constructor.name,
};
Expand Down Expand Up @@ -332,7 +336,11 @@ export function dehydrate(
readonly: true,
preview_short: formatDataForPreview(data, false),
preview_long: formatDataForPreview(data, true),
name: data.constructor.name,
name:
typeof data.constructor !== 'function' ||
typeof data.constructor.name !== 'string'
? ''
: data.constructor.name,
};

getAllEnumerableKeys(data).forEach(key => {
Expand Down

0 comments on commit 133ada7

Please sign in to comment.