Stateless functional components are more simple than class based components and will benefit from future React performance optimizations specific to these components.
This rule will check your class based React components for
- methods/properties other than
displayName,propTypes,renderand useless constructor (same detection as ESLint no-useless-constructor rule) - instance property other than
this.propsandthis.context rendermethod that return anything but JSX (undefined,null, etc.)- presence of
refattribute in JSX
If none of these 4 elements are found, the rule will warn you to write this component as a pure function.
The following pattern is considered warnings:
var Hello = React.createClass({
render: function() {
return <div>Hello {this.props.name}</div>;
}
});The following patterns are not considered warnings:
const Foo = function(props) {
return <div>{props.foo}</div>;
};class Foo extends React.Component {
render() {
if (!this.props.foo) {
return null
}
return <div>{this.props.foo}</div>;
}
}