Git Product home page Git Product logo

babel-plugin-react-displayname's Introduction

@zendesk/babel-plugin-react-displayname

Jest test CI

Automatically generate display names for React components

Installation

Internal usage:

yarn add @zendesk/babel-plugin-react-displayname

Public usage: Copy src/index.js to a local file

@zendesk/babel-plugin-react-displayname: "file:../path/to/file"

Why use this?

React dev tools infer component names from the name of the function or class that defines the component. However, it does not work when anonymous functions are used.

This plugin fixes that by automatically generating the displayName property for assigned anonymous functions.

What does it do?

This plugin converts the following:

const Linebreak = React.memo(() => {
    return <br/>;
});

const Img = function () {
    return <img/>;
}

into:

const Linebreak = React.memo(function _Linebreak() {
  return <br />;
});
Linebreak.displayName = "Linebreak";

const Img = function () {
  return <img />;
};
Img.displayName = "Img";

Install

Using npm:

npm install --save-dev @zendesk/babel-plugin-react-displayname

or using yarn:

yarn add @zendesk/babel-plugin-react-displayname --dev

Usage

With a configuration file (Recommended)

Without options:

{
  "plugins": ["@zendesk/babel-plugin-react-displayname"]
}

With options:

{
  "plugins": ["@zendesk/babel-plugin-react-displayname", {
    "allowedCallees": {
      "react": ["createComponent"]
    }
  }]
}

Options

allowedCallees

Object.<string, string[]>, defaults to { "react": ["createContext"] }

Enables generation of displayNames for certain called functions.

Example

By default, with allowedCallees set to { "react": ["createContext"] }:

import React, { createContext } from 'react';
const FeatureContext = createContext();
const AnotherContext = React.createContext();

is transformed into:

import React, { createContext } from 'react';
const FeatureContext = createContext();
FeatureContext.displayName = "FeatureContext";
const AnotherContext = React.createContext();
AnotherContext.displayName = "AnotherContext";

babel-plugin-react-displayname's People

Contributors

li-kai avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.