Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ESLint no-use-before-define

How do I get this ESLint rule (no-use-before-define) to not warn in cases like these;

class App extends React.Component {
    render() { return <div>{messages.helloWorld}</div> }
}

const messages = { helloWorld: 'Hello world!' }

This is a simplified example, but I'd really like to have messages defined at the bottom of each component's file (by convention).

like image 473
William Avatar asked Mar 23 '17 15:03

William


People also ask

Was used before it was defined Eslint?

To Solve 'React' was used before it was defined Error Just add import/resolver in Eslint setting And Install eslint-import-resolver-typescript if needed: yarn add -D eslint-import-resolver-typescript If that doesn't work, you could change this rule “@typescript-eslint/no-use-before-define”: “off” Now, Your problem ...

How do you ignore Eslint rule?

You can also disable the no-eval rule for an entire function block by using /* eslint-disable */ . If you put /* eslint-disable no-eval */ before any code in a . js file, that will disable the no-eval rule for the entire file. You can also disable all ESLint rules by putting /* eslint-disable */ at the top of a file.

Can you call a function before it has been defined Javascript?

To invoke (call) these functions they always need a variable name. This kind of function won't work if it is called before it has been defined which means Hoisting is not happening here. We must always define the expression function first and then invoke it.


3 Answers

It looks like you might be interested in the variables option, for this rule. You can read about that option here.

This flag determines whether or not the rule checks variable declarations in upper scopes. If this is true, the rule warns every reference to a variable before the variable declaration. Otherwise, the rule ignores a reference if the declaration is in an upper scope, while still reporting the reference if it's in the same scope as the declaration.

You might configure it, in your .eslintrc, like so ...

{
  "no-use-before-define": ["error", { "variables": false }]
}

This will keep that rule enabled for other things, such as classes and functions and variables in the same scope, but will relax it for variables in upper scopes.

like image 87
Ryan Avatar answered Sep 28 '22 15:09

Ryan


Rule disabled for function and classes declarations

"rules": {    
  "no-use-before-define": ["error", {"functions": false, "classes": false}]
}
like image 25
Aga Avatar answered Sep 27 '22 15:09

Aga


Before the render line, do this:

// eslint-disable-next-line no-use-before-define

See the eslint docs.

like image 30
Josh Beam Avatar answered Sep 27 '22 15:09

Josh Beam