Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'React' was used before it was defined

I am working with create-react-app + typescript + eslint application and during build have such error:

Line 1:8:  'React' was used before it was defined  @typescript-eslint/no-use-before-define

Code in my component starts with:

import React from "react";

Eslint settings:

module.exports = {
  parser: "@typescript-eslint/parser",
  parserOptions: {
    ecmaVersion: 2020,
    sourceType: "module",
    ecmaFeatures: {
      jsx: true
    }
  },
  settings: {
    react: {
      version: "detect"
    }
  },
  extends: [
    "plugin:react/recommended",
    "plugin:@typescript-eslint/recommended",
    "prettier/@typescript-eslint",
    "plugin:prettier/recommended"
  ],
  rules: {
    "@typescript-eslint/explicit-module-boundary-types": 0,
    "@typescript-eslint/triple-slash-reference": 0,
    "no-use-before-define": "off",
    "@typescript-eslint/no-use-before-define": "off"
  },
};

Some part of package.json:

"devDependencies": {
  "@typescript-eslint/eslint-plugin": "^4.1.0",
  "@typescript-eslint/parser": "^4.1.0",
  "babel-eslint": "^10.1.0",
  "eslint": "^6.6.0",
  "eslint-config-airbnb": "^18.1.0",
  "eslint-config-prettier": "^6.11.0",
  "eslint-plugin-import": "^2.20.2",
  "eslint-plugin-prettier": "^3.1.3",
  "eslint-plugin-react": "^7.20.0",
  "prettier": "^2.0.5",
  "start-server-and-test": "^1.11.3"
},
"dependencies": {
  ...
  "react-scripts": "3.4.3",
  ...
}

I tried:

  • Read https://github.com/typescript-eslint/typescript-eslint/issues/2502
  • Disable @typescript-eslint/no-use-before-define and no-use-before-define in .eslintrc.js
  • Actually I tried to delete .eslintrc.js at all, but had the same error.
like image 895
Alexey Nazarov Avatar asked Oct 12 '22 10:10

Alexey Nazarov


People also ask

How do you fix React was used before it was defined?

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 ...

When was the React created?

2015 – React is Stable js Conf 2015 – Facebook released the first version of React Native for the React. js Conf 2015 during a technical talk.

Why was ReactJS developed?

React is a JavaScript library developed by Facebook which, among other things, was used to build Instagram.com. Its aim is to allow developers to easily create fast user interfaces for websites and applications alike. The main concept of React. js is virtual DOM.

Is not defined React JSX no undef?

The React. 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' .


1 Answers

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"]
}
like image 306
igor Avatar answered Oct 17 '22 16:10

igor