Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

react/no-unused-class-comp-methods #2166

Closed
jsardev opened this issue Feb 14, 2019 · 21 comments · Fixed by #2239
Closed

react/no-unused-class-comp-methods #2166

jsardev opened this issue Feb 14, 2019 · 21 comments · Fixed by #2239

Comments

@jsardev
Copy link

jsardev commented Feb 14, 2019

I'd like to have a rule to detect unused React class methods. The rule would filter out React lifecycle methods, take the other ones and check if they are used anywhere in the class itself.

Example:

class Component extends React.Component {
    // error: unused method
    someMethod() {
        // something...
    }

    makeApiCall() {
        // do api call...
    }

    handleClick() {
        this.makeApiCall();
    }

    render() {
         return (<button onClick={this.handleClick}>Do it!</button>);
    }
}

The motivation behind this rule is that it's actually a bad practice to reference React.Component instance methods outside of the instance itself, so it would make sense to check if any methods inside the class are being used or are some kind of unused leftover.

@ljharb
Copy link
Member

ljharb commented Feb 14, 2019

You can’t possibly know it’s unused statically - anything that imports Component could reference Component.prototype.someMethod or similar.

@ljharb
Copy link
Member

ljharb commented Feb 14, 2019

Similarly, you never directly use, say, componentDidUpdate, but React does - and there’s no way for a general purpose linter to catch things like that.

@jsardev
Copy link
Author

jsardev commented Feb 14, 2019

You can’t possibly know it’s unused statically - anything that imports Component could reference Component.prototype.someMethod or similar.

I know that, but AFAIK it's not a good practice to reference React.Component static methods outside of the class component itself.

Similarly, you never directly use, say, componentDidUpdate, but React does - and there’s no way for a general purpose linter to catch things like that.

Exactly - you never directly use those and you probably shouldn't also use other component methods outside of it. Knowing this you can filter out React-specific methods (componentDidUpdate etc.) and check if other methods are used in the class itself.

@ljharb
Copy link
Member

ljharb commented Feb 14, 2019

I suppose that might make sense for a React class component, sure. maybe no-unused-class-component-methods

@jsardev
Copy link
Author

jsardev commented Feb 14, 2019

@ljharb Yeah! I'm sorry, maybe I wasn't specific enough about the request :) I've edited my first post to make it more clear.

@jsardev jsardev changed the title Unused class method no-unused-class-component-methods Feb 14, 2019
@jsardev jsardev changed the title no-unused-class-component-methods react/no-unused-class-comp-methods Feb 14, 2019
@n-filatov
Copy link

Can i take this?

@ljharb
Copy link
Member

ljharb commented Feb 26, 2019

Anything with the help wanted label, without a linked PR, is always up for grabs.

@pawelnvk
Copy link
Contributor

I saw that recently there was no action taken regarding this topic. I prepared solution that might help. Let me know, if there is something I should fix there. #2239

@pawelnvk
Copy link
Contributor

@ljharb Do you have time for checking, if this PR is helpful?

@trevorwhealy
Copy link

This would make such a great lint rule! ❤️

@Kiwka
Copy link

Kiwka commented Jun 19, 2019

It would be so wonderful to have this rule 🙏

@BigPun86

This comment has been minimized.

@jamesroyston

This comment has been minimized.

@evegreen

This comment has been minimized.

@mixa9269

This comment has been minimized.

@Waltari10
Copy link

There already is a separate npm plugin released for this, but unfortunately I didn't get it to work. Here it is, if somebody has better luck with it, let me know: https://www.npmjs.com/package/eslint-plugin-no-unused-react-component-methods

@orassayag

This comment has been minimized.

@jakeleventhal

This comment has been minimized.

@jakeleventhal
Copy link
Contributor

jakeleventhal commented Oct 9, 2021

@ljharb
when i try to add this rule it appears to not exist

Definition for rule 'react/no-unused-class-component-methods' was not found react/no-unused-class-component-method
"eslint-plugin-react": "7.26.1"

@ljharb
Copy link
Member

ljharb commented Oct 9, 2021

@jakeleventhal that's because it's not released yet - if you click to the PR, and to the commit that merged it (1371f71) you'll see it's not in any tags.

@jakeleventhal
Copy link
Contributor

@ljharb ah, i was reading the changelog. I didn't realize it was in the "Unreleased" section

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging a pull request may close this issue.