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

Customize display name of custom hooks #15259

Closed
lilactown opened this issue Mar 29, 2019 · 15 comments
Closed

Customize display name of custom hooks #15259

lilactown opened this issue Mar 29, 2019 · 15 comments
Labels
Resolution: Stale Automatically closed due to inactivity Type: Enhancement

Comments

@lilactown
Copy link

Do you want to request a feature or report a bug?

  • Feature

What is the current behavior?

For toolchains/languages that munge the names of functions, the display of custom hooks in React DevTools is unreadable.

For instance, in ClojureScript we might have a custom hook named <-window that would be compiled into the name my_app$hooks$_LT__window.

What is the expected desired behavior?

To have the ability to set a displayName-like value on a custom hook to customize it's name in React DevTools.

A potential solution:

function useSomeReallyLongNameThatIDontWantToShow() {
  // Implement my custom hook
}

useSomeReallyLongNameThatIDontWantToShow.debugName = "useShortName";

useShortName would show up in React DevTools instead of useSomeReallyLongNameThatIDontWantToShow.

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?

React 16.8 / Chrome / macOS 10.13.6

@lilactown
Copy link
Author

Here's an example of a particularly bad case. This is a multi-arity wrapper around useState:

Screen Shot 2019-03-29 at 5 52 03 PM

Should I file an issue in the devtools repo as well as / instead of here?

@gaearon
Copy link
Collaborator

gaearon commented Apr 3, 2019

@gaearon gaearon closed this as completed Apr 3, 2019
@orestis
Copy link

orestis commented Apr 3, 2019 via email

@lilactown
Copy link
Author

Yeah - this is not about the value of the hook but the name.

@gaearon
Copy link
Collaborator

gaearon commented Apr 3, 2019

We get the name from the stack trace so I'm not sure this is even possible. Sounds like you want to ask the engines to respect a field like fn.displayName for stack traces.

@lilactown
Copy link
Author

Hm. I'm ignorant of how hooks work under the hood; it sounds like all we get is the function name and not a reference to it that we could query for a displayName property?

@gaearon
Copy link
Collaborator

gaearon commented Apr 3, 2019

Yep.

@gaearon
Copy link
Collaborator

gaearon commented Apr 3, 2019

FWIW this isn't specific to Hooks — you'd still have the same problem for stack traces. Solving it for stack traces would also solve it in DevTools. So sounds like this is something you'd want to fix at a different level than React.

@gaearon gaearon closed this as completed Apr 3, 2019
@orestis
Copy link

orestis commented Apr 3, 2019 via email

@gaearon
Copy link
Collaborator

gaearon commented Apr 3, 2019

Let's keep it open for discussion. Happy to look at specific proposals.

@gaearon gaearon reopened this Apr 3, 2019
@orestis
Copy link

orestis commented Apr 3, 2019

Thanks :)

Could this be addressed directly in DevTools or would it need changes to React? Just to know where to direct our efforts (and which source to dive into)

@orestis
Copy link

orestis commented Apr 3, 2019

A follow up - could DevTools use source maps to get the original function name?

@stale
Copy link

stale bot commented Jan 10, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contribution.

@stale stale bot added the Resolution: Stale Automatically closed due to inactivity label Jan 10, 2020
@stale
Copy link

stale bot commented Jan 17, 2020

Closing this issue after a prolonged period of inactivity. If this issue is still present in the latest release, please create a new issue with up-to-date information. Thank you!

@stale stale bot closed this as completed Jan 17, 2020
@forivall
Copy link

You can set function.name via the following, and it shows up in devtools and stack traces:

    try {
      Object.defineProperty(useCustom, 'name', {
        value: displayName,
        configurable: true,
      });
    } catch (_) {}

(some environments dont make "name" configurable, so that's why the try/catch is necessary)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Resolution: Stale Automatically closed due to inactivity Type: Enhancement
Projects
None yet
Development

No branches or pull requests

4 participants