Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unexpected use of 'confirm' no-restricted-globals

Tags:

reactjs

I am writing code for the delete confirmation for the user, but when I run it up it shows this error enter image description here

This is my code for that

    onDelete = (id) => {
      console.log(id);
      if (confirm('You want to delette ?')) {
       //eslint-disable-line
        this.props.onDelete(id);
      }
    };

Can anyone explain it to me, Please? Although I have to add //eslint-disable-line but it does not work, my problem in here is when I add //eslint-disable-line beside if (confirm('You want to delette ?')) { like this

`if (confirm('You want to delette ?')) {//eslint-disable-line`

but when I save it, it show like this

if (confirm('You want to delette ?')) {
       //eslint-disable-line

, it automatically down the line, so that the reason display error above, I do not know how to turn of the auto down line when I save

like image 992
Jin Avatar asked Aug 08 '20 04:08

Jin


People also ask

How do I turn off no restricted Globals?

To fix the 'No restricted globals' ESLint Error when developing a React app, we can add the // eslint-disable-next-line no-restricted-globals comment before the code that is causing the error.

What is no restricted Globals?

Rule: no-restricted-globals Disallow specific global variables. Disallowing usage of specific global variables can be useful if you want to allow a set of global variables by enabling an environment, but still want to disallow some of those.


Video Answer


3 Answers

You have to use window.confirm() instead of just confirm().

like image 62
dhruv tailor Avatar answered Oct 24 '22 04:10

dhruv tailor


You can change this part:

if (confirm('You want to delette ?')) {
       //eslint-disable-line
        this.props.onDelete(id);
      }
    };

to:

if (window.confirm('You want to delette ?')) {
       //eslint-disable-line
        this.props.onDelete(id);
      }
    };

Just add window.

like image 11
ahmad wahyu Avatar answered Oct 24 '22 05:10

ahmad wahyu


You can disable this error by adding eslint comment above the confirm() method.

onDelete = (id) => {
  console.log(id);
  // eslint-disable-next-line no-restricted-globals
  if (confirm('You want to delete ?')) {
   //eslint-disable-line
    this.props.onDelete(id);
  }
};

or you can replace the confirm method with window.confirm()

onDelete = (id) => {
  console.log(id);
  if (window.confirm('You want to delete ?')) {
   //eslint-disable-line
    this.props.onDelete(id);
  }
};
like image 3
Codemaker Avatar answered Oct 24 '22 04:10

Codemaker