React Display Name Utility
This package provides a small, reusable utility function, `getDisplayName`, to extract the display name of a React component. It is particularly useful in patterns like Higher-Order Components (HOCs) for creating descriptive `displayName` properties for the wrapped components, aiding in debugging and developer tooling, especially with React DevTools. As of version 0.2.5, it ships with TypeScript definitions, enhancing type safety in projects utilizing TypeScript. The package has a low release cadence, with updates primarily focusing on maintenance, dependency upgrades, or minor feature enhancements like improved default return values. Its core functionality is stable and focused solely on robust component naming conventions.
Common errors
-
TypeError: (0 , react_display_name_1.getDisplayName) is not a function
cause Attempting to destructure `getDisplayName` as a named import when it is a default export in an ESM context.fixChange your import statement from `import { getDisplayName } from 'react-display-name';` to `import getDisplayName from 'react-display-name';`. -
TypeError: getDisplayName is not a function (when using require)
cause Incorrectly trying to access `getDisplayName` as a property of the required module in CommonJS, e.g., `require('react-display-name').getDisplayName`.fixAssign the result of `require` directly: `const getDisplayName = require('react-display-name');` because it's a default export. -
Component name showing as 'Unknown' or generic like 't' in dev tools
cause `getDisplayName` could not reliably infer the component's name, often due to anonymous functions, minification, or missing `displayName` properties.fixExplicitly set the `static displayName = 'YourComponentName';` property on your React components. This provides a robust name that `getDisplayName` can always pick up.
Warnings
- breaking The default string returned when a component's display name cannot be determined changed from 'Component' to 'Unknown'.
- gotcha Anonymous functional components or arrow functions without an assigned name or explicit `displayName` property will default to 'Unknown' (or 'Component' in older versions).
- gotcha Minification and obfuscation tools can sometimes mangle function names, potentially causing `getDisplayName` to return less descriptive or generic names if `displayName` is not explicitly set.
Install
-
npm install react-display-name -
yarn add react-display-name -
pnpm add react-display-name
Imports
- getDisplayName
import { getDisplayName } from 'react-display-name';import getDisplayName from 'react-display-name';
- getDisplayName (CommonJS)
const getDisplayName = require('react-display-name'); - Type Inference
import getDisplayName from 'react-display-name'; // getDisplayName correctly infers types for React components.
Quickstart
import React, { Component } from 'react';
import getDisplayName from 'react-display-name';
const withLogger = (WrappedComponent) => {
class WithLogger extends Component {
static displayName = `WithLogger(${getDisplayName(WrappedComponent)})`;
render() {
console.log(`Rendering ${WithLogger.displayName}`);
return <WrappedComponent {...this.props} />;
}
}
return WithLogger;
};
class MyComponent extends Component {
render() {
return <div>Hello, World!</div>;
}
}
const EnhancedMyComponent = withLogger(MyComponent);
// In a React application, you would render EnhancedMyComponent
// ReactDOM.render(<EnhancedMyComponent />, document.getElementById('root'));
console.log(getDisplayName(MyComponent)); // Expected: 'MyComponent'
console.log(getDisplayName(EnhancedMyComponent)); // Expected: 'WithLogger(MyComponent)'