When you write
```
for (const x of l) {
setTimeout(() => x);
}
```
we need to add a closure because the variable is meant to be block-scoped and recreated each time the block runs. We do this.
However, we also add the closure when no loop is present. This isn't necessary, because if no loop is present then each piece of code runs at most once. I changed the transform to only add a closure if a variable is referenced from within a loop.
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} />
Installation
npm install --save-dev babel-plugin-transform-react-constant-elements
Usage
Via .babelrc (Recommended)
.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"]
});