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

Getting unrelated (react/no-this-in-sfc) violation error #1960

Closed
psonawane-servicetitan opened this issue Aug 28, 2018 · 4 comments · Fixed by #1995
Closed

Getting unrelated (react/no-this-in-sfc) violation error #1960

psonawane-servicetitan opened this issue Aug 28, 2018 · 4 comments · Fixed by #1995

Comments

@psonawane-servicetitan
Copy link

I ran the following simple js program. But I got an error at line this.getA(); which is totally unrelated here. The error said "stateless functional components should not use this (react/no-this-in-sfc)"

class Demo {
    getA = () => 'a';

    hasBug = () => {
        this.getA();

        return null;
    };
}

What did you expect to happen?
No error expected

What actually happened? Please include the actual, raw output from ESLint.
Got this error - stateless functional components should not use this (react/no-this-in-sfc) for line this.getA()

@ljharb
Copy link
Member

ljharb commented Aug 28, 2018

Component detection is probably recognizing it as an SFC since it returns null; this seems like a case where we should have less confidence because a) it's named not in PascalCase, b) it's a class field.

cc @alexzherdev

@alexzherdev
Copy link
Contributor

We're bitten again by the fact that Components.get can return something we're not entirely sure is a component (confidence = 1).
I can make a local fix in this rule to make sure this doesn't happen. But making Components.get always require confidence of 2 breaks 37 different tests in the suite 😕

@ljharb
Copy link
Member

ljharb commented Aug 29, 2018

@alexzherdev in this case it should be reporting a lower confidence than 1, because it's a class field, and because it's not named in PascalCase.

@bendtherules
Copy link

bendtherules commented Aug 30, 2018

Not sure if this is due to the same reason.

Another sample causing unnecessary error:

import React from 'react';

class Extended extends Base {
  testA = () => {
    this.abc(); // Error here - react/no-this-in-sfc

    return (
      <div>
        Hello World
      </div>
    );
  }

  testB() {
    this.abc(); // No error here

    return (
      <div>
        Hello World
      </div>
    );
  }
}

It doesn't happen inside non-arrow functions

sergei-startsev added a commit to sergei-startsev/eslint-plugin-react that referenced this issue Sep 24, 2018
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.

4 participants