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
@babel/plugin-transform-react-inline-elements
This plugin transforms JSX elements to object literals like React.createElement and is similar to @babel/plugin-transform-react-constant-elements in that it aims to improve performance. However, it inlines elements instead of hoisting them, which can be beneficial in some cases but not as effective for elements that could be hoisted outside of a high-frequency render path.
babel-plugin-transform-react-remove-prop-types
This plugin removes unnecessary React propTypes from the production build. It is similar in its goal to optimize React applications for production by reducing bundle size, but it does not specifically optimize render performance like @babel/plugin-transform-react-constant-elements does.
babel-plugin-react-pure-components
This plugin converts React.Component classes that do not use state or lifecycle methods into functional components. While it also aims to optimize React components, it focuses on a different aspect of optimization by enabling potential function component optimizations rather than hoisting static elements.
@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
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
Via .babelrc
(Recommended)
.babelrc
{
"plugins": ["@babel/plugin-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/plugin-transform-react-constant-elements", {"allowMutablePropsOnTags": ["FormattedMessage"]}],
]
}
Via CLI
babel --plugins @babel/plugin-transform-react-constant-elements script.js
Via Node API
require("@babel/core").transform("code", {
plugins: ["@babel/plugin-transform-react-constant-elements"]
});
References