Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resolve the no-undef error for setTimeout

My code

  componentDidMount() {
    // we add a hidden class to the card and after 700 ms we delete it and the transition appears
    this.timeOutFunction = setTimeout(
      function () {
        this.setState({cardAnimaton: ""});
      }.bind(this),
      700
    );
  }

  componentWillUnmount() {
    clearTimeout(this.timeOutFunction);
    this.timeOutFunction = null;
  }

  componentWillMount() {
    if (this.state.logged_in) {
      this.props.history.push("/dashboard");
    }
  }

I turned on the following in my .eslintrc

{
    "parser": "babel-eslint",
    "plugins": [
        "react"
    ],
    "extends": [
        "eslint:recommended",
        "plugin:react/recommended"
    ],
    "rules": {
       "no-set-state": "off",
       "react/no-multi-comp":  [1, { "ignoreStateless": true }]
    },

    "parserOptions": {
      "ecmaVersion": 6,
      "sourceType": "module",
      "ecmaFeatures": {
          "jsx": true,
          "modules": true
      }
  },
  "globals": {
    "localStorage": true,
    "fetch": true
},
  "settings": {
    "react": {
        "pragma": "React",
        "version": "16.4.1"
    }
    }
}

And I get the following warnings

setTimeout is not defined (no-undef)
clearTimeout is not defined (no-undef)

How do I resolve this warning?

like image 448
Kim Stacks Avatar asked Sep 24 '18 04:09

Kim Stacks


People also ask

Is not defined no undef?

js error "X is not defined react/jsx-no-undef" occurs when we forget to import a function, class or a variable in our code before using it. To solve the error, make sure to import the value before using it in your code, e.g. import {myFunc} from 'my-package' . Copied!

How do you set setTimeout in react?

How to use setTimeout in React. The setTimeout function accepts two arguments: the first is the callback function that we want to execute, and the second specifies the timeout in milliseconds before the function will be called. setTimeout(() => console. log('Initial timeout!'

How do you stop a setTimeout loop?

To stop a setTimeout loop with JavaScript, we can call the clearTimeout function. let timeOutVar; const myFunc = (terminator = false) => { if (terminator) { clearTimeout(timeOutVar); } else { timeOutVar = setTimeout(myFunc, 1000); } }; myFunc(true); myFunc(false);


1 Answers

The issue is your environment in .eslintrc is not configured.

Each environment brings with it a certain set of predefined global variables.

You can configure it for browser [your React/Redux/JavaScript Code] & node [Webpack and build related code].

"env": {
  "browser": true,
  "node": true
},
like image 191
Ravi Roshan Avatar answered Sep 20 '22 05:09

Ravi Roshan