Turn JSX elements into direct function calls or objects.
See this: 45% Faster React Functional Components, Now
In
const Avatar = ({url}) =>
<div class="avatar">
<img src={url} />
</div>
const element = <Avatar url={avatarUrl} />
Out
const Avatar = ({ url }) => React.createElement(
"div",
{ "class": "avatar" },
React.createElement("img", { src: url })
);
-const element = React.createElement(Avatar, { url: avatarUrl });
+const element = Avatar({ url: avatarUrl });
Works with stateless functional components(render functions) only.
Enable ensureFunctional
option if there are stateful(classful) components in the app.
There's no sCU
for stateless functional components.
If a component is expensive to update, consider using pure
HOC, pureComponent
or classful components with custom sCU
for it.
HOCs can be used if the result component is not warpped with class
. These HOCs doesn't wrap components with class
:
branch()
defaultProps()
flattenProp()
getContext()
hoistStatics()
mapProps()
next()
renameProp()
renameProps()
renderComponent()
setDisplayName()
setPropTypes()
setStatic()
withProps()
wrapDisplayName()
Developer tools won't show inlined components.
React internals are required to use these.
import PropTypes from 'prop-types'
const handleProps = component => props => {
const mergedProps = Object.assign({}, component.defaultProps, props)
PropTypes.checkPropTypes(component.propTypes, mergedProps, 'prop', component.displayName)
return component(mergedProps)
}
npm i --save-dev babel-plugin-transform-react-jsx babel-plugin-transform-inline-elements
.babelrc.js
module.exports = {
plugins: ['transform-react-jsx', 'transform-inline-elements']
}
babel --plugins transform-react-jsx transform-inline-elements script.js
require('babel-core').transform('code', {
plugins: ['transform-react-jsx', 'transform-inline-elements']
});
boolean
, defaults to false
.
Call components directly only if they are functions.
Enable this when using stateful components.
In
import Avatar from './avatar'
const element = <Avatar url={avatarUrl} />
Out
import { ensureFunctional as _ensureFunctional } from 'babel-plugin-transform-inline-elements/es/helpers.js';
import Avatar from './avatar';
const _Avatar = _ensureFunctional(React.createElement, Avatar);
const element = _Avatar({ url: avatarUrl });