question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Component name as prop

See original GitHub issue

Feature Request

Component name as props

Current behavior

You have to set .displayName in a second statement

const MyComponent = (props) => <Stuff />;

MyComponent.displayName = 'HeyHey'

https://stackoverflow.com/questions/43356073/how-to-set-displayname-in-a-functional-component-react

Desired behavior

You can set a component’s displayName by setting a displayName prop, ideally anywhere in the component hierarchy, like so:

export default props => <Stuff displayName="MyStuff" />;

or so:

export default props => (
  <Fragment displayName="FragmentWithName">
    <Stuff displayName="HeyHey" />
  </Fragment>
);

Why

To make writing components less verbose

Other ideas: https://github.com/facebook/react-devtools/issues/1294

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
gaearoncommented, Mar 26, 2019

You have to set .displayName in a second statement

You don’t “have to” do it. The question you referenced is about production builds. If you just need it in development, it’s enough to give names to your functions.

const Foo = () => ...

export default Foo

or

export default function Foo() {
  // ...
}

The only reason you have this problem is because you do export default props => .... That’s not a recommended pattern because none of your JS functions will appear properly even in regular JS stacktraces.

1reaction
revskill10commented, Mar 26, 2019

I think let’s just keep default behavior, but allow overriding the displayName with props. Best of three worlds.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React / JSX Dynamic Component Name - Stack Overflow
A decorator can be used as higher-order component with functional components: const Bar = props => ...; Bar.displayName = 'Bar'; ...
Read more >
React — Dynamic Component Names with JSX - Medium
In React, names starting with a capital letter will compile to the createComponent method. ... const TagName = this.components[this.props.tag || 'foo'];
Read more >
Components and Props - React
Conceptually, components are like JavaScript functions. They accept arbitrary inputs (called “props”) and return React elements describing what should appear on ...
Read more >
Dynamic Tag Name Props in React (with TypeScript)
Sometimes, a React component needs to allow users to render a custom tag. Here's how you can pass dynamic tag names as props....
Read more >
React component as prop: the right way™️
Exploring how to pass React components as props, why we would want to do it, and what is the best pattern for the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found