hichem
hichem

Reputation: 121

react.js 'x' is assigned a value but never used no-unused-vars

I've setup eslint & eslint-plugin-react.

When I run ESLint, the linter returns no-unused-vars .

I'm assuming it's not recognizing that I'm using JSX or React syntax. Any ideas?

Line 5:11: 'x' is assigned a value but never used no-unused-vars

Example:

Please help me


Inside a file Component

import React,{ Component } from 'react';
class Item extends Component{
   render () {
       const x = 1;
       return (
         <div>test</div>
       );
    }
};

export default Item;

Inside a file .eslintrc.json

{
"env": {
    "browser": true,
    "es6": true
},
"extends": [
    "eslint:recommended",
    "plugin:react/recommended",
    "react-app","prettier"
],
"settings": {
  "react": {
    "createClass": "createReactClass"
    "pragma": "React",
    "version": "detect",
    "flowVersion": "0.53"
  },
  "propWrapperFunctions": [
      "forbidExtraProps",
      {"property": "freeze", "object": "Object"},
      {"property": "myFavoriteWrapper"}
  ],
  "linkComponents": [
    "Hyperlink",
    {"name": "Link", "linkAttribute": "to"}
  ]
},
"parserOptions": {
    "ecmaVersion": 2018,
    "ecmaFeatures": {
      "jsx": true
    }
},
"plugins": [
    "react","prettier"
],
"rules": {
  "react/jsx-uses-react": "error",
  "react/jsx-uses-vars": "error",
  "no-unused-vars": ["error", { "vars": "all", "args": "after-used", "ignoreRestSiblings": false }],
}

}

Upvotes: 2

Views: 47571

Answers (3)

Awshaf Ishtiaque
Awshaf Ishtiaque

Reputation: 931

In production you may do differently, but you can always add a line before your variable declaration to ignore the warnings:

// eslint-disable-next-line

Upvotes: 1

Danish Patel
Danish Patel

Reputation: 1

{
    "env": {
        "browser": true,
        "es6": true
    },
    "extends": [
        "eslint:recommended",
        "plugin:react/recommended",
        "react-app","prettier"
    ],
    "settings": {
      "react": {
        "createClass": "createReactClass",
        "pragma": "React",
        "version": "detect",
        "flowVersion": "0.53"
      },
      "propWrapperFunctions": [
          "forbidExtraProps",
          {"property": "freeze", "object": "Object"},
          {"property": "myFavoriteWrapper"}
      ],
      "linkComponents": [
        "Hyperlink",
        {"name": "Link", "linkAttribute": "to"}
      ]
    },
    "parserOptions": {
        "ecmaVersion": 2018,
        "ecmaFeatures": {
          "jsx": true
        }
    },
    "plugins": [
        "react","prettier"
    ],
    "rules": {
      "react/jsx-uses-react": "error",
      "react/jsx-uses-vars": "error",
      "no-unused-vars": ["error", { "vars": "all", "args": "after-used", "ignoreRestSiblings": false }]
    }
}

Upvotes: 0

Dmitry Sobolevsky
Dmitry Sobolevsky

Reputation: 1191

ESLint lint behavior is right. You've declared x but don't use in your JSX. It should disappear if use it:)

import React,{ Component } from 'react';
class Item extends Component{
   render () {
       const x = 1;
       return (
         <div>test {x}</div>
       );
    }
};

export default Item;

Upvotes: 2

Related Questions