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.
npm install react-display-nameVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to use `getDisplayName` within a Higher-Order Component (HOC) to construct a descriptive `displayName` for debugging purposes.
If your application relied on the exact 'Component' string for fallback logic, update your comparisons to 'Unknown' or ensure your components have explicit `displayName` properties.
Always provide a `displayName` static property for your React components, especially when using HOCs or `React.memo`, to ensure consistent and debuggable naming across environments and build steps. Example: `MyComponent.displayName = 'MyComponent';`
Always explicitly set the `displayName` static property on your React components to ensure predictable naming, regardless of build optimizations. This is particularly important for library authors.
Change your import statement from `import { getDisplayName } from 'react-display-name';` to `import getDisplayName from 'react-display-name';`.Assign the result of `require` directly: `const getDisplayName = require('react-display-name');` because it's a default export.Explicitly set the `static displayName = 'YourComponentName';` property on your React components. This provides a robust name that `getDisplayName` can always pick up.
No dependency data recorded yet.