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

[no-access-state-in-setstate] inspects non-react classes #2464

Closed
fabiospampinato opened this issue Oct 14, 2019 · 5 comments · Fixed by #2711
Closed

[no-access-state-in-setstate] inspects non-react classes #2464

fabiospampinato opened this issue Oct 14, 2019 · 5 comments · Fixed by #2711

Comments

@fabiospampinato
Copy link

fabiospampinato commented Oct 14, 2019

The react/no-access-state-in-setstate rule can find errors in non-react classes, which doesn't make a lot of sense.

In my specific use case I'm using overstated for state management, which provides a similar API to React's own API for updating the state, which trips this rule.

It's impossible in general to statically detect if a class is a react component or not, e.g. class Foo extends Abstract {}, who knows if that's a react component or not.

Therefor I propose adding an option for blacklisting specific classes, so that I can tell the rule that "Abstract" in my example above is not a react class.

@ljharb
Copy link
Member

ljharb commented Oct 16, 2019

Can you provide some specific code that's detected as a React component?

@fabiospampinato
Copy link
Author

@ljharb for example:

class Foo extends Abstract {

  update = () => {

    const result = this.getResult ( this.state.foo );

    return this.setState ({ result });

  };

}

@ljharb
Copy link
Member

ljharb commented Oct 16, 2019

Certainly that shouldn't be detected as a React component, thanks.

@fabiospampinato
Copy link
Author

fabiospampinato commented Oct 16, 2019

Certainly that shouldn't be detected as a React component, thanks.

I mean it depends, how do you know Abstract isn't something that extends React.Component for instance? I don't think you can really tell with static analysis.

@ljharb
Copy link
Member

ljharb commented Oct 16, 2019

Sure, but a) it is in incredibly discouraged and bad pattern to use inheritance hierarchies in React, so you shouldn't be doing that anyways; and b) there's a jsdoc "extends" comment you're already required to use in those situations to opt your component into being detected by this plugin.

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.

2 participants