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

no-access-state-in-setstate bug? #1597

Closed
fohrloop opened this issue Dec 11, 2017 · 1 comment · Fixed by #1598
Closed

no-access-state-in-setstate bug? #1597

fohrloop opened this issue Dec 11, 2017 · 1 comment · Fixed by #1598

Comments

@fohrloop
Copy link

Hi,

The react/no-access-state-in-setstate rule should prevent usage of this.state inside setState calls. I can see the it generating error with the following code:

Example with errors / trigger

import React from 'react'

class Test extends React.Component {
  state = { count: 0 }

  handleClick = () => {
    this.setState({ count: this.state.count + 1 })
  }
  render() {
    return <p onClick={this.handleClick}>{this.state.count}</p>
  }
}

export default Test

but when the state variable is destructured from this.state, there will be no errors/warnings:

Example without errors / trigger

import React from 'react'

class Test extends React.Component {
  state = { count: 0 }

  handleClick = () => {
    const { state } = this
    this.setState({ count: state.count + 1 })
  }
  render() {
    const { state } = this
    return <p onClick={this.handleClick}>{state.count}</p>
  }
}

export default Test

This is especially troublesome when using react/destructuring-assignment with the react/no-access-state-in-setstate.

System & setup

Windows 10, node v.8.9.3, eslint-plugin-react v. 7.5.1, ESLint v.3.19.0 & babel-eslint 8.0.3, using the rule 'react/no-access-state-in-setstate': 2.

@jaaberg
Copy link
Contributor

jaaberg commented Dec 11, 2017

Nice find!

I had to dive into this, and hopefully fixed it in #1598.
Also made this while fiddling around to show that it works. Hope this helps :)

ljharb added a commit that referenced this issue Dec 13, 2017
Find edge case in no-access-state-in-setstate (#1597)
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