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-set-state error does not trigger when in an arrow function's scope #931

Closed
kevinSuttle opened this issue Oct 27, 2016 · 1 comment
Closed
Labels

Comments

@kevinSuttle
Copy link

kevinSuttle commented Oct 27, 2016

Snippets where it is not detected:

toggle = () => {
    const { visible } = this.state;
    // No error thrown
    this.setState({ visible: !visible });

    const hookEvent = !visible ? document.addEventListener : document.removeEventListener;
    hookEvent('mousedown', this.close);
    hookEvent('keydown', this.toggle);
  }
onMouseEnter={() => this.setState({ dropdownIndex: index })}

Snippet where it is detected and throws an error (same file as above)

keyDown = (e) => {
    const keycode = e.keyCode || e.Which,
      { dropdownIndex } = this.state

    switch (keycode) {
      case 13 /* ENTER */:
        if (this.state.optionsShown) {
          this.selectedItem(dropdownIndex)
        }
        break
      case 38 /* UP */:
      case 40 /* DOWN */:
        if (!this.state.optionsShown) {
          this.toggle()
        } else {
          const { options } = this.props,
            down = keycode === 40,
            adjustIndex = (index) => index < 0 ? options.length - 1 :
              index >= options.length ? 0 : index

         // Throws error, not in 
          this.setState({ dropdownIndex: adjustIndex(dropdownIndex + (down ? 1 : -1)) },
            this.focusCurrentItem)
        }
        return e.preventDefault()
    }
  }

.eslintrc.json

{
  "parser": "babel-eslint",
  "parserOptions": {
    "ecmaVersion": 2017,
    "sourceType": "module",
    "ecmaFeatures": {
      "classes": false,
      "jsx": true
    }
  },
  "plugins": [
    "import",
    "react",
    "jsx-a11y"
  ],
  "extends": [
    "airbnb",
    "eslint:recommended",
    "plugin:react/recommended"
  ],
  "globals": {
    "document": true,
    "window": true
  },
  "rules": {
    "react/no-unused-prop-types": ["warn", {"skipShapeProps": true}],
    "react/no-set-state": 2,
    "react/prefer-stateless-function": 2,
    "import/extensions": [2, "never", { "js": "never", "jsx": "never", "json": "never"}],
    "import/newline-after-import": 0,
    "import/no-extraneous-dependencies": [
      "off",
      {
        "devDependencies": false,
        "optionalDependencies": false
      }
    ],
    "react/jsx-filename-extension": [1, { "extensions": [".js", ".jsx"] }]
  }
}
@kevinSuttle kevinSuttle changed the title no-set-state error does not trigger when an arrow function's scope no-set-state error does not trigger when in an arrow function's scope Oct 27, 2016
@yannickcr yannickcr added the bug label Nov 1, 2016
@kevinSuttle
Copy link
Author

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants