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

False positive no-access-state-in-setstate when using this.state in a function called in the setstate callback #1931

Closed
Aequitas737 opened this issue Aug 14, 2018 · 6 comments

Comments

@Aequitas737
Copy link

Related to #1610, which doesn't completed catch all the cases.

With version 7.10.0 when the callback calls a function which references this.state.

myFunc = () => {
   this.setState({
            ...prop,
        }, () => this.doStuff());
    }

    doStuff= () => {
        console.log(this.state.asdf);
    }

Can work around this by just passing this.state into doStuff: this.doStuff(this.state)

@ljharb
Copy link
Member

ljharb commented Aug 14, 2018

You definitely don't ever want to pass around the props or state object; we need to get this fixed.

@eric-wood
Copy link

Also seeing this, and have found I can work around it by using destructuring:

this.setState({ ...stuff }, () => {
  const { asdf } = this.state;
  console.log(asdf);
});

@jaybekster
Copy link

Has it been fixed?

@ljharb
Copy link
Member

ljharb commented Feb 20, 2019

@jaybekster nope, that's why the issue is still open

@Jexah
Copy link

Jexah commented Aug 2, 2019

+1 for interest in fixing.

@jzabala
Copy link
Contributor

jzabala commented Jul 11, 2020

Working properly with versions:

"babel-eslint": "^10.1.0"
"eslint": "^7.4.0"
"eslint-plugin-react": "^7.20.3"

Screen Shot 2020-07-11 at 11 12 09 AM

@ljharb ljharb closed this as completed in 253a62a Jul 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

6 participants