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-state triggered when optional chaining is used #2515

Closed
gustavolaux opened this issue Dec 6, 2019 · 8 comments · Fixed by #2588
Closed

react/no-unused-state triggered when optional chaining is used #2515

gustavolaux opened this issue Dec 6, 2019 · 8 comments · Fixed by #2588

Comments

@gustavolaux
Copy link

gustavolaux commented Dec 6, 2019

eslint: 6.5.1
eslint-plugin-react: 7.16.0
babel-eslint: 10.0.3

in constructor:

this.state = {
  downloading: false,
}

but when this.state?.downloading is used i got this message:

image

@ljharb
Copy link
Member

ljharb commented Dec 6, 2019

Presumably you're using babel-eslint since optional chaining just got stage 4 within the last 10 hours; it seems reasonable to modify all the rules that check for dotted and bracketed things to also check for optionally dotted and bracketed things.

@gustavolaux
Copy link
Author

edited with babel-eslint version

@JoshuaKGoldberg
Copy link

Sorry to nitpick, but is this an enhancement or a bug?

@ljharb
Copy link
Member

ljharb commented Mar 3, 2020

Actually I'm not sure it's either.

this.state?.downloading doesn't make any sense to me - when would this.state be nullish?

@JoshuaKGoldberg
Copy link

State is null by default, so this is something you need to do if you don't give it an initial value.

https://codesandbox.io/s/react-state-null-initially-ui5rp:

class App extends React.Component {
  render() {
    return (
      <div>{JSON.stringify(this.state)}</div>
    );
  }
}

@ljharb
Copy link
Member

ljharb commented Mar 3, 2020

well sure, but you're always supposed to give it an initial value - does this.setState even work if you don't?

@JoshuaKGoldberg
Copy link

Yup, it works! React doesn't give any warning or indication, so I'd assume there are many (like me) who relied on the behavior.

@ljharb
Copy link
Member

ljharb commented Mar 10, 2020

Oof, that sounds awful :-/ hopefully #2588 will resolve this, either way.

ljharb pushed a commit to golopot/eslint-plugin-react that referenced this issue May 9, 2020
ljharb pushed a commit to golopot/eslint-plugin-react that referenced this issue May 9, 2020
ljharb pushed a commit to golopot/eslint-plugin-react that referenced this issue May 9, 2020
@ljharb ljharb closed this as completed in da7a045 May 9, 2020
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.

3 participants