简体   繁体   English

带有 eslint-config-airbnb 的扩展名为“.js”的文件中不允许使用 JSX

[英]JSX not allowed in files with extension ' .js' with eslint-config-airbnb

I've installed eslint-config-airbnb that is supposed to pre configure ESLINT for React:我已经安装了eslint-config-airbnb ,它应该为 React 预配置 ESLINT:

Our default export contains all of our ESLint rules, including ECMAScript 6+ and React.我们的默认导出包含我们所有的 ESLint 规则,包括 ECMAScript 6+ 和 React。 It requires eslint, eslint-plugin-import, eslint-plugin-react, and eslint-plugin-jsx-a11y.它需要 eslint、eslint-plugin-import、eslint-plugin-react 和 eslint-plugin-jsx-a11y。

My .eslintrc extending its configuration:我的.eslintrc扩展了它的配置:

{ "extends": "eslint-config-airbnb",
  "env": {
    "browser": true,
    "node": true,
    "mocha": true
  },
  "rules": {
    "new-cap": [2, { "capIsNewExceptions": ["List", "Map", "Set"] }],
    "react/no-multi-comp": 0,
    "import/default": 0,
    "import/no-duplicates": 0,
    "import/named": 0,
    "import/namespace": 0,
    "import/no-unresolved": 0,
    "import/no-named-as-default": 2,
    "comma-dangle": 0,  // not sure why airbnb turned this on. gross!
    "indent": [2, 2, {"SwitchCase": 1}],
    "no-console": 0,
    "no-alert": 0,
    "linebreak-style": 0
  },
  "plugins": [
    "react", "import"
  ],
  "settings": {
    "import/parser": "babel-eslint",
    "import/resolve": {
      "moduleDirectory": ["node_modules", "src"]
    }
  },
  "globals": {
    "__DEVELOPMENT__": true,
    "__CLIENT__": true,
    "__SERVER__": true,
    "__DISABLE_SSR__": true,
    "__DEVTOOLS__": true,
    "socket": true,
    "webpackIsomorphicTools": true
  }
}

Unfortunatelly I'm getting the following error when linting a.js file with React JSX code inside it:不幸的是,在对其中包含 React JSX 代码的 .js 文件进行 linting 处理时出现以下错误:

 error  JSX not allowed in files with extension '.js'              react/jsx-filename-extension

Wasn't eslint-config-airbnb configured react to support JSX already, as stated?如前所述,eslint-config-airbnb 是否已配置为支持 JSX?

What should be done to remove that error?应该怎么做才能消除该错误?

Either change your file extensions to .jsx as mentioned or disable the jsx-filename-extension rule.将您的文件扩展名更改为.jsx或禁用jsx-filename-extension规则。 You can add the following to your config to allow .js extensions for JSX.您可以将以下内容添加到您的配置中以允许 JSX 的.js扩展。

"rules": {
  "react/jsx-filename-extension": [1, { "extensions": [".js", ".jsx"] }],
}

It's work for me.这对我有用。 hope to help you.希望能帮到你。

  1. disable jsx-filename-extension in .eslintrc :.eslintrc禁用 jsx-filename-extension :

    "rules": { "react/jsx-filename-extension": [0] } “规则”:{“反应/jsx-文件名-扩展名”:[0]}

  2. in webpack.config.js :webpack.config.js

    resolve: { extensions: ['.js', '.jsx'] },解析:{ 扩展名:['.js', '.jsx'] },

Call me a dummy if it does not work for you如果它不适合你,请叫我傻瓜

    "rules": {
        "react/jsx-filename-extension": [0],
        "import/extensions": "off"
    }

If you don't want to change your file extension, you can export a function that returns jsx, and then import and call that function in your js file.如果不想更改文件扩展名,可以导出返回 jsx 的函数,然后在 js 文件中导入并调用该函数。

// greeter.jsx

import React from 'react';

export default name => (
  <div>
    {`Hello, ${name}!`}
  </div>
);

and then进而

// index.js

import ReactDOM from 'react-dom';
import greeter from './components/greeter';

const main = document.getElementsByTagName('main')[0];

ReactDOM.render(greeter('World'), main);

Following React documentation :以下React 文档

Each JSX element is just syntactic sugar for calling React.createElement(component, props, ...children).每个 JSX 元素只是调用 React.createElement(component, props, ...children) 的语法糖。

Following Airbnb's style guide :遵循Airbnb的风格指南

Do not use React.createElement unless you're initializing the app from a file that is not JSX .不要使用 React.createElement ,除非你从一个不是 JSX 的文件初始化应用程序

You could do this:你可以这样做:

    //index.js
    const app = React.createElement(App);
    ReactDOM.render(app, document.getElementById('root'));

To expound on Martin's answer, it seems that it is not possible, currently, to use JSX in React Native.为了阐述Martin 的回答,目前似乎不可能在 React Native 中使用JSX A PR was created but reverted due to concerns about fragmentation and unknown consequences of having things like index.ios.jsx . PR已创建,但由于担心碎片化和具有index.ios.jsx类的未知后果而index.ios.jsx I'm not sure how AirBnb works around this or if they do, but I have used basically the same rules block as Martin.我不确定 AirBnb 是如何解决这个问题的,或者他们是否这样做,但我使用了与 Martin 基本相同的rules块。

For future readers who want to write jsx in .js files:对于未来想要在.js文件中编写 jsx 的读者:

  1. Install npm i react react-dom even if you think you're not writing react.安装npm i react react-dom即使你认为你不是在写 react。
  2. Install npm i -D @babel/preset-react安装npm i -D @babel/preset-react
  3. In babel config: plugins: ['@babel/plugin-transform-react-jsx']在 babel 配置中: plugins: ['@babel/plugin-transform-react-jsx']
  4. you should setup module.rules for /\.jsx?$/ files with babel-loader (so you will need to install npm i -D babel-loader too)您应该使用babel-loader/\.jsx?$/文件设置module.rules (因此您也需要安装npm i -D babel-loader

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM