'React' was used before it was defined
If you are only getting this error for .js
files, make sure @typescript-eslint/parser
is being used exclusively on Typescript files.
.eslintrc.json (abbreviated)
{
"overrides": [
{
"files": ["**/*.ts", "**/*.tsx"],
"plugins": ["@typescript-eslint"],
"rules": {
"no-use-before-define": "off",
"@typescript-eslint/no-use-before-define": ["error"],
},
}
],
// WRONG: Do not use @typescript-eslint/parser on JS files
// "parser": "@typescript-eslint/parser",
"plugins": [
"react",
// "@typescript-eslint"
],
}
That is how I resolved my problem.
- First of all, go to the node_modules/react-scripts/config/webpack.config.js and change cache to false, because this will help you to understand what works and what is not then you changing eslint file. I found it here
cache: true, // You can set it to false
formatter: require.resolve('react-dev-utils/eslintFormatter'),
- Add ENV varialbe to .env file. More info
EXTEND_ESLINT=true
- From now CRA will have to use your local eslint for linting during your build and we have control to disable some rules, in my case in .eslintrc:
rules: {
"@typescript-eslint/no-use-before-define": "off"
},
From the official documentation.
"rules": {
// note you must disable the base rule as it can report incorrect errors
"no-use-before-define": "off",
"@typescript-eslint/no-use-before-define": ["error"]
}
The bug occurs due to mismatch of @typescript-eslint
versions in react-scripts and your local package.json
- GitHub issue
You can downgrade the packages until react-scripts
updates their version.
"@typescript-eslint/eslint-plugin": "4.0.1",
"@typescript-eslint/parser": "4.0.1",
EDIT 2020-09-14
It seems the bug is not related to react-scripts
version of @typescript-eslint
since multiple people reported the same bug without using react-scripts
.
Anyway, the workaround remains the same - downgrade to the working version of @typescript-eslint
until the fix is available.
EDIT 2020-10-24
[email protected]
has been published with updated @typescript-eslint
. Using the newest version should solve the issue.
EDIT 2020-11-04
If after upgrading the packages the error is still there, most probably your eslint config uses the wrong rule. Check out Igor's answer to fix it.