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).
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 ...
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.
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.
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.
"rules": {
"no-use-before-define": ["error", {"functions": false, "classes": false}]
}
Before the render
line, do this:
// eslint-disable-next-line no-use-before-define
See the eslint docs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With