ESLint is giving me this error on a react project.
ESLint - Component should be written as a pure function (react prefer/stateless function)
It points to the first line of the component.
export class myComponent extends React.Component {
render() {
return (
//stuff here
);
}
}
How do I get rid of this error?
Add constructor() like:
If you rely on
props
, then there is a better (somewhat arguable, as of this writing) way to fix this error without writing out Stateless functions - by writing a PureComponent and using this eslint rule [source]:With above rule, the following snippet is valid (since it depends on
props
)React team plans to build optimizations around SFC but they are not there yet. So until that happens,
SFCs
will not offer any benefits overPureComponents
. In fact, they will be slightly worse as they will not prevent wasteful renders.