Component name as prop
See original GitHub issueFeature 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:
- Created 4 years ago
- Reactions:1
- Comments:5 (1 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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.
or
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.I think let’s just keep default behavior, but allow overriding the
displayName
withprops
. Best of three worlds.