使用JSX react / react-in-jsx-scope时,“反应”必须在范围内?


129

我是Angular开发人员,是React的新手,这是简单的react Component,但无法正常工作

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;

错误: 使用JSX react / react-in-jsx-scope时,“ React”必须在范围内

Answers:


250

导入行应为:

import React, { Component }  from 'react';

注意React的大写字母R。


3
如何避免。我的意思是,当我创建一个无状态的功能,在Nextjs它不需要它
Muhaimin CS

1
@MuhaiminCS更改eslintrc文件中的规则
帕特里克


12

将以下设置添加到.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会为您完成此操作。


NextJs用户在这里,谢谢。给定添加规则"react/react-in-jsx-scope": "off"将消除错误,添加将globals完成什么工作?谢谢!
DeBraid

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.