Skip to content

[Bug]: no-this-in-sfc reports false positives on inner functions that are not function components #3994

@juona

Description

@juona

Is there an existing issue for this?

  • I have searched the existing issues and my issue is unique
  • My issue appears in the command-line and not only in the text editor

Description Overview

The react/no-this-in-sfc rule reports an error if a function component has some internal function that uses this that refers to an entity other than the function component.

const MyComponent = () => {
  function doMagik(this: any, arg: any) {
    this.value = arg; // error here
  }

  class Internal {
    private value: number;

    constructor() {
      this.value = 10; // and here
    }
  }

  doMagik(new Internal());

  return <div />;
};

IMHO whether writing code this way is a "good practice" is a separate concern that could be addressed by a separate linting rule or a config option under this one. The point is that the rule as it stands today reports a false positive. If I understand its intent correctly, that is.

Thank you!

Expected Behavior

The rule should either:

  • not report any errors in the above scenarios;
  • have a flag that would allow the developer to adjust its behavior so that it does not report any errors in the above scenarios.

eslint-plugin-react version

^7.37.5

eslint version

v9.39.4

node version

v24.14.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions