-
Notifications
You must be signed in to change notification settings - Fork 47.4k
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 value formatting of proxies of class instances #30880
base: main
Are you sure you want to change the base?
Conversation
For Hookstate Proxies of classes, `data.constructor.name` returns `Proxy({})`, so use `Object.getPrototypeOf(data).constructor.name` instead, which works correctly from my testing.
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
return data.constructor.name; | ||
return Object.getPrototypeOf(data).constructor.name; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would rather change it to something like this:
let resolvedConstructorName = data.constructor.name;
if (typeof resolvedConstructorName === 'string') {
return resolvedConstructorName;
}
resolvedConstructorName = Object.getPrototypeOf(data).constructor.name;
if (typeof resolvedConstructorName === 'string') {
return resolvedConstructorName;
}
try {
return truncateForDisplay(String(data));
} catch (error) {
return 'unserializable';
}
What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have a feeling that other stuff should be inside the try
block as well, but otherwise, that looks good to me :) Do you have push access to my branch ("Allow edits by maintainers" is checked) or would you like me to commit this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please push it? You can also wrap everything in try block, yeah
For Hookstate Proxies of class instances,
data.constructor.name
returnsProxy({})
, so useObject.getPrototypeOf(data).constructor.name
instead, which works correctly from my testing.Summary
React DevTools immediately bricks itself if you inspect any component that has a prop that is a Hookstate that wraps a class instance ... because these are proxies where
data.constructor.name
returns some un-cloneable object, butObject.getPrototypeOf(data)
doesn't returnObject
(it returns the prototype of the class inside).How did you test this change?
This part of the code has no associated tests at all.
Technically,
packages/react-devtools-shared/src/__tests__/legacy/inspectElement-test.js
exists, but I triedyarn test
and these tests aren't even executed anymore. I can't figure it out, so whatever.If you run this code:
then
instanceProxy.constructor.name
returns some proxy that cannot be cloned, butObject.getPrototypeOf(instanceProxy).constructor.name
returns the correct value.This PR fixes the devtools to use
Object.getPrototypeOf(instanceProxy).constructor.name
.I modified my local copy of devtools to use this method and it fixed the bricking that I experienced.
Related #29954