`class-methods-use-this`: "exceptMethods" array option
See original GitHub issueWhat 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:
- Created 7 years ago
- Reactions:11
- Comments:20 (18 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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:
Bad code without the option, or with an empty array:
Bad code with the option:
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
andgetDefaultProps
, I actually thought about it but figured it was only for thecreateClass
approach, which is obviously not true, I’ve just grown so accustomed to class properties already. Thanks for pointing these out!