Socket
Socket
Sign inDemoInstall

@babel/plugin-transform-react-constant-elements

Package Overview
Dependencies
0
Maintainers
1
Versions
79
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @babel/plugin-transform-react-constant-elements

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


Version published
Weekly downloads
4.8M
decreased by-18.06%
Maintainers
1
Created
Weekly downloads
 

Package description

What is @babel/plugin-transform-react-constant-elements?

The @babel/plugin-transform-react-constant-elements package is a Babel plugin that optimizes React applications by hoisting constant elements to the highest scope, which can reduce the need to recreate these elements on every render. This can result in performance improvements, especially for components that render the same static elements repeatedly.

What are @babel/plugin-transform-react-constant-elements's main functionalities?

Hoisting constant elements

This plugin will hoist the static elements <h1>Header</h1> and <p>Content</p> out of the render method, so they are only created once and reused on subsequent renders.

class MyComponent extends React.Component {
  render() {
    return (
      <div>
        <h1>Header</h1>
        <p>Content</p>
      </div>
    );
  }
}

Other packages similar to @babel/plugin-transform-react-constant-elements

Readme

Source

@babel/plugin-transform-react-constant-elements

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

This plugin can speed up reconciliation and reduce garbage collection pressure by hoisting React elements to the highest possible scope, preventing multiple unnecessary reinstantiations.

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} />
    
  • Mutable Properties

See https://github.com/facebook/react/issues/3226 for more on this

<div width={{width: 100}} />

Installation

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

Usage

.babelrc

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

Options

allowMutablePropsOnTags

Array<string>, defaults to []

If you are using a particular library (like react-intl) that uses object properties, and you are sure that the element won't modify its own props, you can whitelist the element so that objects are allowed.

This will skip the Mutable Properties deopt.

{
  "plugins": [
    ["@babel/transform-react-constant-elements", {"allowMutablePropsOnTags": ["FormattedMessage"]}],
  ]
}

Via CLI

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

Via Node API

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

References

Keywords

FAQs

Last updated on 30 Oct 2017

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc