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

[Bug]: react/no-unused-class-component-methods instance property React element false positive #3599

Open
2 tasks done
jwoo92 opened this issue Jul 8, 2023 · 11 comments
Open
2 tasks done
Labels

Comments

@jwoo92
Copy link

jwoo92 commented Jul 8, 2023

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

After upgrading from babel-eslint@^10.1.0 to @babel/eslint-parser@^7.22.7, using an instance property as a React element leads to a react/no-unused-class-component-methods false positive.

no-unused-class-component-methods_false-positive

Steps to Replicate

class ViewModel extends React.Component {
  constructor(props) {
    super(props);

    this.View = View;
  }

  render() {
    return <this.View />;
  }
}

Using the example code above, run the following command in your CLI:

eslint . --ext .js,.jsx

Expected Behavior

ESLint should not return an error.

eslint-plugin-react version

v7.23.2

eslint version

v8.42.0

node version

v18.16.1

@jwoo92 jwoo92 added the bug label Jul 8, 2023
@ljharb
Copy link
Member

ljharb commented Jul 8, 2023

That's a really strange usage pattern - why does it need to be stored on the instance, instead of just referenced via closure?

@jwoo92
Copy link
Author

jwoo92 commented Jul 8, 2023

While a bare minimum example, real-world use includes legacy class-based view models before functional/hooks became trendy.

@ljharb
Copy link
Member

ljharb commented Jul 8, 2023

React class components are never subclassed by anything, and this.View isn't set to anything dynamic, so I'm still not sure why that would have ever been a reasonable pattern.

@jwoo92
Copy link
Author

jwoo92 commented Jul 8, 2023

this.View is so that the view component is not seen as unique causing premature re-rendering.

@ljharb
Copy link
Member

ljharb commented Jul 19, 2023

@jwoo92 i have no idea what that means - React only ever considers props and state and component identity, and ignores instance properties, and that's always been the case.

@jwoo92
Copy link
Author

jwoo92 commented Jul 19, 2023

@ljharb Jordan, thank you for helping me comprehend your viewpoint on this design pattern. I'd be happy to discuss it further with you separately from the issue at hand.

To keep our conversation on track, I have a couple examples of the package behavior and false positive.

class ViewModel extends React.Component {
  constructor(props) {
    super(props);

    this.View = View; // No ESLint error
  }

  render() {
    const { View } = this;

    return <View />;
  }
}
class ViewModel extends React.Component {
  constructor(props) {
    super(props);

    this.View = View; // ESLint false positive
  }

  render() {
    return <this.View />;
  }
}

Regarding this regression, would you be the individual to collaborate with to assist in its resolution?

@ljharb
Copy link
Member

ljharb commented Jul 19, 2023

@jwoo92 it's not a design pattern that i've ever seen, and it's actively harmful in that it will make your code slower for precisely zero benefit, and it's the reason you're running into a false positive. There's no benefit in fixing a false positive that nobody would ever run into (since I can't imagine anyone else writing code this way).

In other words, the best solution seems to be fixing your code to not do that.

@jwoo92
Copy link
Author

jwoo92 commented Jul 19, 2023

@ljharb are you replying to my comment above, or is our timing a coincidence?

@ljharb
Copy link
Member

ljharb commented Jul 19, 2023

I'm replying to your comment.

@jwoo92
Copy link
Author

jwoo92 commented Jul 19, 2023

@ljharb Thank you for clarifying.

There appears to be a misunderstanding here. I'm open to discussing the design pattern further.

I have followed the pull request guidelines and provided supporting evidence. Could you please state your needs and the subsequent steps we should take to make headway?

@ljharb
Copy link
Member

ljharb commented Jul 19, 2023

First I'm trying to understand why anyone would do this.X = X for any class X, not even React-specific - every class already has this.constructor === X for that scenario anyways. Second, I'm trying to understand why you'd do that in react, where it 100% guaranteed never has or will have a rendering impact.

If we can establish that the code that gets a false positive shouldn't exist in the first place then the proper path forward is for you to change your code, and we don't need to bother fixing it here.

Alternatively, if the use case is valid, then we should fix it here.

Can you help me understand why you would do <this.View> instead of just <View> from inside the class? They'll be === identical, so React (and JS) can't possibly tell the difference.

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

No branches or pull requests

2 participants