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 value formatting of proxies of class instances #30880

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

LoganDark
Copy link
Contributor

@LoganDark LoganDark commented Sep 4, 2024

For Hookstate Proxies of class instances, data.constructor.name returns Proxy({}), so use Object.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, but Object.getPrototypeOf(data) doesn't return Object (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 tried yarn test and these tests aren't even executed anymore. I can't figure it out, so whatever.

If you run this code:

    class Class {}
    const instance = new Class();

    const instanceProxy = new Proxy(instance, {
      get(target, key, receiver) {
        if (key === 'constructor') {
          return { name: new Proxy({}, {}) };
        }

        return Reflect.get(target, key, receiver);
      },
    });

then instanceProxy.constructor.name returns some proxy that cannot be cloned, but Object.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

For Hookstate Proxies of classes, `data.constructor.name` returns `Proxy({})`, so use `Object.getPrototypeOf(data).constructor.name` instead, which works correctly from my testing.
Copy link

vercel bot commented Sep 4, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
react-compiler-playground ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 5, 2024 2:11am

Copy link

@anandfresh anandfresh left a comment

Choose a reason for hiding this comment

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

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants