react-display-name
TypeScript icon, indicating that this package has built-in type declarations

0.2.5 • Public • Published
npm install --save react-display-name

Get the displayName from a Component. This is a common pattern with React Higher Order Components (HoCs). This is a simple reusable utility to get the name of a component.

Usage:

import {expect} from 'chai';
import React, {Component} from 'react';
import getDisplayName from 'react-display-name';
 
const container = (WrappedComponent) => {
  class Container extends Component {
    static displayName = `Container(${getDisplayName(WrappedComponent)})`;
    render() {
      return (
        <WrappedComponent />
      );
    }
  }
  return Container;
}
 
class HelloWorld extends Component {
  render() {
    return (
      <div>Hello</div>
    );
  }
}
 
const HelloWorldPrime = container(HelloWorld);
 
expect(getDisplayName(HelloWorldPrime)).to.equal('Container(HelloWorld)');
expect(HelloWorldPrime.displayName).to.equal('Container(HelloWorld)');

Package Sidebar

Install

npm i react-display-name

Weekly Downloads

375,433

Version

0.2.5

License

MIT

Unpacked Size

4.01 kB

Total Files

5

Last publish

Collaborators

  • jurassix