Skip to content

Support setting Context displayName in @babel/plugin-transform-react-display-name #11241

Closed

Description

@kentcdodds

Feature Request

  • I would like to work on this feature!
  • I have bandwidth to work on this feature in the near future. 🙁

Is your feature request related to a problem? Please describe.

Here's an example of React DevTools with a few context providers and consumers:

image

You'll notice that the context consumer and provider rendered under ThemeProvider have generic Context.Consumer and Context.Provider display names, but there's also a Router.Provider and Router.Consumer in there which are much more clear.

They accomplish this by setting the context's displayName value.

Here are some test cases:

import React from 'react'

const MyContext = React.createContext()
MyContext.displayName = 'MyContext'

Describe the solution you'd like

I'd like @babel/plugin-transform-react-display-name to support automatically setting the displayName property for contexts.

Describe alternatives you've considered

I can set it manually. Not desirable. Or I could write my own babel plugin, but then others wouldn't benefit from it if they either don't know about the plugin or can't install it. Also, this seems to make a lot of sense to be included here to me.

Teachability, Documentation, Adoption, Migration Strategy

No need, it'll just magically start working and people will be pleasantly surprised.

Activity

  1. kevin940726 commented on Mar 11, 2020

    @kevin940726

    I would love to help :) !

    However, I think @babel/plugin-transform-react-display-name only supports assigning displayName to components created by createClass? I'm not sure if it should be the right target package we should build this into? Maybe I'm missing something 🤔

  2. kentcdodds commented on Mar 11, 2020

    @kentcdodds
    MemberAuthor

    Yes, I believe that's true. That said, there's nothing in the name to suggest that all it handles and considering it's already used by projects which would want this feature I think it's an acceptable place to put this.

  3. kevin940726 commented on Mar 11, 2020

    @kevin940726

    @kentcdodds Fair enough 👍! I'm just curious then why babel-plugin-add-react-displayname isn't also part of this?

  4. kentcdodds commented on Mar 11, 2020

    @kentcdodds
    MemberAuthor

    I'm also curious about that. I definitely prefer the trade-off of a few extra bytes to have a better production debugging experience.

  5. nicolo-ribaudo commented on Mar 11, 2020

    @nicolo-ribaudo
    Member

    Oh wow, I had no idea that our plugin was so outdated.

    @roncohen Would you be open to adding this feature to babel-plugin-add-react-displayname, and then merging babel-plugin-add-react-displayname to the official Babel plugin?

  6. existentialism commented on Mar 11, 2020

    @existentialism
    Member

    Related: #8748

  7. ahutchings commented on Nov 14, 2020

    @ahutchings

    Related: #5679

  8. added
    outdated
    on Nov 3, 2021
  9. locked as resolved and limited conversation to collaborators on Nov 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: reactoutdated

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions