Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.eslintrc.js for React 17 and JSX without import 'react'

Tags:

reactjs

eslint

in react 17 is not necessarily use

import React from 'react';

but if i don't have it, so eslint gave me error

'React' must be in scope when using JSX react/react-in-jsx-scope

any idea how modify .eslintrc.js

module.exports = {
  parser: "babel-eslint",
  env: {
    browser: true,
    node: true,
    es6: true,
    jest: true,
  },
  extends: [
    "eslint:recommended",
    "plugin:react/recommended",
    "plugin:jsx-a11y/recommended"
    ],
  plugins: [
    "react",
    "react-hooks",
    "jsx-a11y",
  ],
  rules: {
    strict: 0,
    "react-hooks/rules-of-hooks": "error",
    "react-hooks/exhaustive-deps": "warn"
  },
  settings: {
    react: {
      version: "detect"
    }
  }
}

for react 17?

Thank's a lot

like image 504
Petr Mašát Avatar asked Nov 02 '20 13:11

Petr Mašát


People also ask

Can you use JSX without importing React?

Starting from React 17, JSX transform use special jsx function internally. You don't need to import it.

Do I need to import React in React 17?

You no longer need to import React from "react" . Starting from the release 17 of React, JSX is automatically transformed without using React.

Is it necessary to import React?

Without it, the transformed JSX code would fail in the browser as React needs to be in scope to create components through its createElement function.

How do I enable JSX in React?

To use JavaScript inside of JSX, you need to surround it with curly braces: {} . This is the same as when you added functions to attributes. To create React components, you'll need to convert the data to JSX elements. To do this, you'll map over the data and return a JSX element.


2 Answers

You need to add plugin:react/jsx-runtime to extends in the .eslintrc.js file.

like is:

module.exports = {
  extends: [
    'plugin:react/recommended',
    'airbnb',
    'plugin:react/jsx-runtime',
  ]
}

Refer here

like image 22
wind8866 Avatar answered Oct 29 '22 05:10

wind8866


You can read about it in React docs.

If you are using eslint-plugin-react, the react/jsx-uses-react and react/react-in-jsx-scope rules are no longer necessary and can be turned off or removed.

{
  // ...
  "rules": {
    // ...
    "react/jsx-uses-react": "off",
    "react/react-in-jsx-scope": "off"
  }
}
  • react-in-jsx-scope on github.

To make it work, you should add those rules to your eslint config, see Extending or replacing the default ESLint config for Create-React-App specifics, every framework should have related section in their docs.

like image 91
Dennis Vash Avatar answered Oct 29 '22 06:10

Dennis Vash