Giter Site home page Giter Site logo

leehomeok / babel-plugin-transform-react-class-to-function Goto Github PK

View Code? Open in Web Editor NEW

This project forked from remcohaszing/babel-plugin-transform-react-class-to-function

0.0 0.0 0.0 1.33 MB

A Babel 7 plugin which transforms React component classes into functions

License: MIT License

JavaScript 100.00%

babel-plugin-transform-react-class-to-function's Introduction

babel-plugin-transform-react-class-to-function

A Babel 7 plugin which transforms React component classes into functions

npm version build status codecov

Writing React components using the class syntax has several benefits:

  • Consistency — Define all components using similar syntax.
  • Static properties — Components are more self contained when using static class properties.
  • Simpler diffs — No need to change the entire indentation converting between classes and functions.

There is one obvious downside:

  • Size — Class components are larger than function components.

This plugin solves that for you. 😃

Example

In

import PropTypes from 'prop-types';
import { Component } from 'react';

export class HelloWorld extends Component {
  static propTypes = {
    className: PropTypes.string,
  };

  render() {
    const { className } = this.props;

    return <div className={className}>Hello world!</div>;
  }
}

Out

import PropTypes from 'prop-types';
import { Component } from 'react';

export const HelloWorld = ({ className }) => <div className={className}>Hello world!</div>;

HelloWorld.propTypes = {
  className: PropTypes.string,
};

Installation

npm install @babel/core babel-plugin-transform-react-class-to-function

Usage

Via babel.config.js (Recommended)

module.exports = (api) => ({
  plugins: ['babel-plugin-transform-react-class-to-function'],
});

Via CLI

babel --plugins babel-plugin-transform-react-class-to-function

Via Node API

require('@babel/core').transform(code, {
  plugins: ['babel-plugin-transform-react-class-to-function'],
});

Options

memo

  • true: Transform PureComponent and components implementing shouldComponentUpdate to functional components using React memo.
  • false (default): Don’t transform PureComponent or components implementing shouldComponentUpdate.

Special Thanks

This plugin was originally based on babel-plugin-transform-react-pure-class-to-function. However, the project has diverged a lot. You may want to give that project a try if you need to use babel 6.

babel-plugin-transform-react-class-to-function's People

Contributors

doochik avatar remcohaszing 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.