Eslint-plugin-react: [prefer-stateless-function] ignores static propTypes and defaultProps

Created on 25 May 2018  路  3Comments  路  Source: yannickcr/eslint-plugin-react

Similar to the issue in #1521, I am experiencing this with both defaultProps and propTypes.

Versions

eslint-plugin-react: 7.8.2
react-native: 0.55.3

Example

export default class MyClass extends React.Component {
  static defaultProps = {
    style: null,
  }

  static propTypes = {
    style: ViewPropTypes.style,
  }

  render() { // stuff }
}

The following results in:

Component should be written as a pure function.

question

All 3 comments

Indeed, as it should - you should do this instead:

function MyClass() {
  // stuff
}
MyClass.defaultProps = {
  style: null,
};
MyClass.propTypes = {
  style: ViewPropTypes.style,
};
export default MyClass;

Why is that preferred?

@offero because if it doesn't need to be a class, it shouldn't be - functional components are always preferred because they have no inherent mutable state.

Was this page helpful?
0 / 5 - 0 ratings