Eslint-plugin-react: Arrow function, PureComponent and prefer-stateless-function

Created on 11 Oct 2016  路  4Comments  路  Source: yannickcr/eslint-plugin-react

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
bug

Most helpful comment

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} />
        }
      }
    }
}

All 4 comments

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} />
        }
      }
    }
}
Was this page helpful?
0 / 5 - 0 ratings