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

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy