@zhigang1992/babel-plugin-react-anonymous-display-name
TypeScript icon, indicating that this package has built-in type declarations

0.1.1 • Public • Published

babel-plugin-react-anonymous-display-name

Motivation

alt text

Babel plugin that fixes displaying, in react devtools, components wrapped by React.memo and forwardRef as Anonymous.

Install

Using npm:

npm install --save-dev babel-plugin-react-anonymous-display-name

or using yarn:

yarn add babel-plugin-react-anonymous-display-name --dev

How does this work?

If you also prefer using arrow functions the only way to get proper component names in react devtools right now is:

// doesn't work :(
export const Memo = React.memo(() => <div />);
Memo.displayName = 'Memo';

// works
const MyComponent = React.memo(function MyComponent(props) {
  return <div />;
});

// works too
const MemoComponent = () => <div />;
export const Memo = React.memo(MemoComponent);

But it leads to the unnecessary code and in bigger projects I can be an issue. This plugin fixes the issue by transforming anonymous arrow function into named function with name taken from the variable

const Memo = React.memo(() => <div />);

into:

const Memo = React.memo(function Memo() {
  return <div />;
});

Babel Config

{
  plugins: [
    ["babel-plugin-react-anonymous-display-name", {
      "hocs": ['memo', 'forwardRef'], // optional
    }]
  ]
}

Eslint plugin

As you don't have to set displayName manually anymore, here is Eslint plugin that will help you to find places where you defined displayName on memo() components:

License

MIT

Dependents (0)

Package Sidebar

Install

npm i @zhigang1992/babel-plugin-react-anonymous-display-name

Weekly Downloads

25

Version

0.1.1

License

MIT

Unpacked Size

6.05 kB

Total Files

7

Last publish

Collaborators

  • zhigang1992