Skip to content

Allow setting a displayName for wrapper component created in renderHook #1396

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

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

Conversation

vikr01
Copy link

@vikr01 vikr01 commented Apr 19, 2025

What:

  • Adding a displayName so that if you try to inspect the wrapper component renderHook creates, you can (i.e. console.log, useFiber, ReactCurrentOwner)

Why:

  • It's a simple change and makes debugging smoother for those who need it

How:

Checklist:

  • Documentation added to the
    docs site
  • Tests
  • TypeScript definitions updated
  • Ready to be merged

Copy link

codesandbox-ci bot commented Apr 19, 2025

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 443834a:

Sandbox Source
react-testing-library-examples Configuration

@MatanBobi
Copy link
Member

Thanks for this.
My main concern with this PR is that people might use it and assert on it for some reason. What's the real benefit you get from seeing the display name here? E.g. what's the use case where you felt like you need it?

@vikr01
Copy link
Author

vikr01 commented Apr 19, 2025

For hooks that use the internal fiber nodes, such as https://github.com./pmndrs/its-fine, when you check the react tree, you'd want to see a name you're familiar with otherwise it's just going to be null.

https://github.com./pmndrs/its-fine/blob/main/src/index.test.tsx#L146

In my specific use case, I'm creating a hook that gives you a string that is unique to the structure of an element, so that you can restore values based on its position (i.e. after suspending). So in this scenario, I want this actual displayName string because the pointer for the component is destroyed.

https://github.com./vikr01/react-suspense-tools/blob/initial_build/packages/useStructuralId/src/__tests__/__snapshots__/useStructuralId.test.tsx.snap#L3

So yes, it is something you would want to assert on, but you can only assert on it if you're able to reach the component by attempting to see the element tree.

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

Successfully merging this pull request may close these issues.

2 participants