babel/packages/babel-plugin-transform-react-constant-elements
Scott Kyle c438209718 Fix constant elements hoisted before declarator (#4804)
When multiple declarators are present in a declaration, we want to insert the constant element inside the declaration rather than placing it before because it may rely on a declarator inside that same declaration.
2016-12-03 20:19:32 -08:00
..
2015-10-29 17:51:24 +00:00
2016-05-29 15:50:04 -04:00

babel-plugin-transform-react-constant-elements

Treat React JSX elements as value types and hoist them to the highest scope

Example

In

const Hr = () => {
  return <hr className="hr" />;
};

Out

const _ref = <hr className="hr" />;

const Hr = () => {
  return _ref;
};

Deopts

  • Spread Operator

    <div {...foobar} />
    
  • Refs

    <div ref="foobar" />
    <div ref={node => this.node = node} />
    
  • Composite Components

    const ComponentA = () => <div><MyCustomComponent /></div>;
    

Installation

npm install --save-dev babel-plugin-transform-react-constant-elements

Usage

.babelrc

{
  "plugins": ["transform-react-constant-elements"]
}

Via CLI

babel --plugins transform-react-constant-elements script.js

Via Node API

require("babel-core").transform("code", {
  plugins: ["transform-react-constant-elements"]
});

References