I write some error decorator.
If it returns like this, there is no problem:
import React, {PureComponent, PropTypes} from 'react'
export default function errorDecorator (options) {
return WrappedComponent => {
class Wrapper extends PureComponent {
static propTypes = {
error: PropTypes.string
}
render () {
const {error, ...props} = this.props
if (error) {
return <div>Error! {error}</div>
} else {
return <WrappedComponent {...props} />
}
}
}
return Wrapper
}
}
but with concise syntax I got error:
export default function errorDecorator (options) {
return WrappedComponent =>
class Wrapper extends PureComponent { // Error: Component should be written as a pure function
static propTypes = {
error: PropTypes.string
}
render () {
const {error, ...props} = this.props
if (error) {
return <div>Error! {error}</div>
} else {
return <WrappedComponent {...props} />
}
}
}
}
UPD:
.eslintrc
parser: babel-eslint
env:
browser: true
node: true
plugins:
- react
extends:
- plugin:react/recommended
rules:
react/prefer-es6-class:
- 2
- always
react/prefer-stateless-function:
- 2
- ignorePureComponents: true
The bug is that it should be warning in both cases.
Sorry, forgot to show .eslintrc
Ah - if you put React.PureComponent would it behave correctly in both cases?
Nope, same output
export default function errorDecorator (options) {
return WrappedComponent =>
class Wrapper extends React.PureComponent { // Error: Component should be written as a pure function
static propTypes = {
error: PropTypes.string
}
render () {
const {error, ...props} = this.props
if (error) {
return <div>Error! {error}</div>
} else {
return <WrappedComponent {...props} />
}
}
}
}
Most helpful comment
Nope, same output