Skip to content

add displayName for React elements #8748

Open

Description

@StipJey

Next code

import React from 'react'

const Arrow = () => <div />

Arrow.beta = () => <div ><p /> </div>

Arrow.gamma = function Gamma() {
  return <div />
}

class Zet extends React.Component {
  render() {
    return <div />
  }
}

transpiled to

import React from "react";

const Arrow = () => React.createElement("div", null);

Arrow.beta = () =>
  React.createElement("div", null, React.createElement("p", null), " ");

Arrow.gamma = function Gamma() {
  return React.createElement("div", null);
};

class Zet extends React.Component {
  render() {
    return React.createElement("div", null);
  }
}

but I want way to have next result:

import React from "react";

const Arrow = () => React.createElement("div", null);
Arrow.displayName = 'Arrow'

Arrow.beta = () =>
  React.createElement("div", null, React.createElement("p", null), " ");
Arrow.beta.displayName  = 'Arrow.beta'

Arrow.gamma = function Gamma() {
  return React.createElement("div", null);
};
Arrow.gamma .displayName  = 'Arrow.gamma'

class Zet extends React.Component {
  render() {
    return React.createElement("div", null);
  }
}
Zet.displayName = 'Zet'

Maybe the solution already exists, but I could not find it

Activity

  1. babel-bot commented on Sep 21, 2018

    @babel-bot
    Collaborator

    Hey @StipJey! We really appreciate you taking the time to report an issue. The collaborators
    on this project attempt to help as many people as possible, but we're a limited number of volunteers,
    so it's possible this won't be addressed swiftly.

    If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack
    community that typically always has someone willing to help. You can sign-up here
    for an invite.

  2. emersonlaurentino commented on Oct 15, 2018

    @emersonlaurentino
    Contributor
  3. StipJey commented on Oct 22, 2018

    @StipJey
    Author

    Unfortunately, this does not solve the problem. This preset works with the creatReactClass and not with React.createElement

  4. isaachinman commented on Dec 10, 2019

    @isaachinman

    Is there any update on options that are available to devs for this use case?

  5. donaldpipowitch commented on Dec 10, 2019

    @donaldpipowitch
  6. igorpupkinable commented on Feb 14, 2021

    @igorpupkinable

    I started working on something. It should be quite easy to extend, PR welcome. https://github.com/prisma-capacity/babel-plugin-react-display-name

    Thank you @donaldpipowitch for a great work! This solved the problem with our shared components library where Enzyme shallow renderer took component names from obfuscated code.

  7. donaldpipowitch commented on Feb 15, 2021

    @donaldpipowitch

    I'm happy that this package was useful :)

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

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions