在JSX中使用'React'时,必须包含在范围内 react/react-in-jsx-scope
P粉364642019
P粉364642019 2023-09-18 19:58:41
0
2
573

我是一个Angular开发者,对React还不熟悉,这是一个简单的React组件,但是不起作用。

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/react-in-jsx-scope

P粉364642019
P粉364642019

全部回复(2)
P粉953231781

.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速查表)

P粉183077097

导入行应该是:

import React, { Component }  from 'react';

注意React的大写R。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!