Heim  >  Fragen und Antworten  >  Hauptteil

Bei Verwendung von „React“ in JSX muss es im Bereich „react/react-in-jsx-scope“ enthalten sein

Ich bin ein Angular-Entwickler und neu bei React. Dies ist eine einfache React-Komponente, aber sie funktioniert nicht.

import react, { Component } from 'react';
import { render } from 'react-dom';

class TechView extends Component {

    constructor(props){
       super(props);
       this.state = {
           name:'Gopinath'
       }
    }
    render(){
        return(
            <span>hello Tech View</span>
        );
    }
}

export default TechView;

Fehler: Bei Verwendung von JSX muss „React“ im Bereich „react/react-in-jsx-scope“ liegen

P粉364642019P粉364642019367 Tage vor577

Antworte allen(2)Ich werde antworten

  • P粉953231781

    P粉9532317812023-09-19 00:40:20

    .eslintrc.js / .eslintrc.json中添加以下设置以忽略这些错误:

    rules: {
        // suppress errors for missing 'import React' in files
       "react/react-in-jsx-scope": "off",
        // allow jsx syntax in js files (for next.js project)
       "react/jsx-filename-extension": [1, { "extensions": [".js", ".jsx"] }], //should add ".ts" if typescript project
      }

    为什么? 如果您使用的是NEXT.js,则无需在文件顶部导入React,nextjs会为您完成。

    参考:https://gourav.io/blog/nextjs-cheatsheet(Next.js速查表)

    Antwort
    0
  • P粉183077097

    P粉1830770972023-09-19 00:36:48

    导入行应该是:

    import React, { Component }  from 'react';
    

    注意React的大写R。

    Antwort
    0
  • StornierenAntwort