Content-Length: 283477 | pFad | https://redirect.github.com/jsx-eslint/eslint-plugin-react/issues/1597

9D no-access-state-in-setstate bug? · Issue #1597 · jsx-eslint/eslint-plugin-react · GitHub
Skip to content

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

@fohrloop

Description

@fohrloop

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions









      ApplySandwichStrip

      pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


      --- a PPN by Garber Painting Akron. With Image Size Reduction included!

      Fetched URL: https://redirect.github.com/jsx-eslint/eslint-plugin-react/issues/1597

      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy