question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`class-methods-use-this`: "exceptMethods" array option

See original GitHub issue

What version are you using? 3.4.0

What did you do? Enabled class-methods-use-this and ran it on this code:

import React from 'react';

class Foo extends React.Component {
  componentDidMount() {
    doSomethingHereWithLogging();
  }
}

What happened? Expected 'this' to be used by class method 'componentDidMount'

What did you expect to happen? What I would like to happen is to be able to specify “componentDidMount”, and all the other React lifecycle methods, as exceptions to this rule. In other words, the typical reasoning for a class method is that it needs access to this - but in the case of React components, the lifecycle methods provide context-based hooks that don’t necessarily require this. A generic “exceptMethods” array would allow me to manually specify component lifecycle methods, and exempt them from this rule, without losing its general utility.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:11
  • Comments:20 (18 by maintainers)

github_iconTop GitHub Comments

8reactions
ljharbcommented, Sep 9, 2016

Essentially: "class-methods-use-this": [2, { "exceptMethods": ["componentDidMount"] }], where “exceptMethods” defaulted to [] and was always concatenated to ["constructor"] (ie, the default behavior).

Good code with the option:

class Foo extends React.Component {
  componentDidMount() {
    doSomethingHereWithLogging();
  }
}

Bad code without the option, or with an empty array:

class Foo extends React.Component {
  componentDidMount() { // warning here
    doSomethingHereWithLogging();
  }
}

class Bar extends React.Component {
  componentDidMount() { // warning here
    doSomethingHereWithLogging();
  }

  foo() { // warning here
    return 3;
  }
}

Bad code with the option:

class Foo extends React.Component {
  componentDidMount() {
    doSomethingHereWithLogging();
  }

  foo() { // warning here
    return 3;
  }
}
3reactions
mathieumgcommented, Sep 29, 2016

There’s a few more for React, that eslint- config-airbnb defines.

For reference, https://github.com/airbnb/javascript/blob/8468ed842314a5d66816927ba6c35f018035cffc/packages/eslint-config-airbnb/rules/react.js#L22-L33

I don’t use external configurations, that’s why posted my snippet. 😃 That being said, I forgot about context (it’s not on the React lifecycle page since it’s still technically not officially supported) and I use class properties that’s why I didn’t include getInitialState and getDefaultProps, I actually thought about it but figured it was only for the createClass approach, which is obviously not true, I’ve just grown so accustomed to class properties already. Thanks for pointing these out!

Read more comments on GitHub >

github_iconTop Results From Across the Web

class-methods-use-this - ESLint - Pluggable JavaScript Linter
The exceptMethods option allows you to pass an array of method names for which you would like to ignore warnings. For example, you...
Read more >
Array methods - The Modern JavaScript Tutorial
Returns the array of removed elements. This method is easy to grasp by examples.
Read more >
class-methods-use-this - Rules - ESLint - GitHub Pages
The exceptMethods option allows you to pass an array of method names for which you would like to ignore warnings. Examples of incorrect...
Read more >
Ruby - Executing same code after most methods in a class
I have classes with more than 60 methods that I have hard-coded the same piece of code that ispasted all over the place....
Read more >
ArrayList (Java Platform SE 8 ) - Oracle Help Center
In addition to implementing the List interface, this class provides methods to manipulate the size of the array that is used internally to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found