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

后端 未结 6 902
执笔经年
执笔经年 2020-12-04 12:08

I\'ve installed eslint-config-airbnb that is supposed to pre configure ESLINT for React:

Our default export contains all of our ESLint rules, includin

相关标签:
6条回答
  • 2020-12-04 12:09

    Call me a dummy if it does not work for you

        "rules": {
            "react/jsx-filename-extension": [0],
            "import/extensions": "off"
        }
    
    0 讨论(0)
  • 2020-12-04 12:13

    It's work for me. hope to help you.

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

      "rules": { "react/jsx-filename-extension": [0] }

    2. in webpack.config.js:

      resolve: { extensions: ['.js', '.jsx'] },

    0 讨论(0)
  • 2020-12-04 12:17

    To expound on Martin's answer, it seems that it is not possible, currently, to use JSX in React Native. A PR was created but reverted due to concerns about fragmentation and unknown consequences of having things like 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.

    0 讨论(0)
  • 2020-12-04 12:25

    Either change your file extensions to .jsx as mentioned or disable the jsx-filename-extension rule. You can add the following to your config to allow .js extensions for JSX.

    "rules": {
      "react/jsx-filename-extension": [1, { "extensions": [".js", ".jsx"] }],
    }
    
    0 讨论(0)
  • 2020-12-04 12:29

    Following React documentation:

    Each JSX element is just syntactic sugar for calling React.createElement(component, props, ...children).

    Following Airbnb's style guide:

    Do not use React.createElement unless you’re initializing the app from a file that is not JSX.

    You could do this:

        //index.js
        const app = React.createElement(App);
        ReactDOM.render(app, document.getElementById('root'));
    
    0 讨论(0)
  • 2020-12-04 12:31

    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.

    // 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);
    
    0 讨论(0)
提交回复
热议问题