What is babel-plugin-add-react-displayname?
The babel-plugin-add-react-displayname npm package is a Babel plugin that automatically adds a displayName to React components for easier debugging in development tools. This is particularly useful for components defined as anonymous functions, which otherwise would not have a displayName set, making them appear as 'Unknown' in React Developer Tools.
What are babel-plugin-add-react-displayname's main functionalities?
Automatic displayName addition
This feature automatically adds a displayName property to React components defined as anonymous functions. This helps in identifying components in debugging tools.
const MyComponent = () => <div>Hello</div>;
// Transformed by babel-plugin-add-react-displayname
// MyComponent.displayName = 'MyComponent';
Other packages similar to babel-plugin-add-react-displayname
babel-plugin-transform-react-display-name
Similar to babel-plugin-add-react-displayname, this package also adds display names to React components. However, it supports a broader range of component definitions and is more widely used in the community.
react-display-name
This package provides a utility to manually assign display names to React components. Unlike babel-plugin-add-react-displayname, which automates the process during the build step, react-display-name requires manual intervention in the code.
babel-plugin-add-react-displayname
Automatically detects and sets displayName for React components.
This is useful for having real component names show up in production builds of React apps.
Babel already does this for React.createClass
style components, this adds support for the two other kinds of component definitions:
- ES6-classes style components
- Stateless components that return JSX
Installation
Simply add add-react-displayname
to your .babelrc
file:
{
"plugins": ["add-react-displayname"]
}
Troubleshooting
Doesn't work for decorated classes
If you are using the transform-decorators-legacy
plugin, make sure it's placed after this plugin in your plugin list.
Testing
npm test