首页 > 解决方案 > 导入组件时使用 JSX 错误时,get React' 必须在范围内

问题描述

我正在编写此代码并在文件中导入React和并尝试将 app.js 导入到. 但我得到这个''React'在使用时必须在范围内Componentapp/jsindex.jserrorJSXindex.js

应用程序.js:

import React ,{Component} from 'react';

class App extends Component{
    constructor(props){
        super(props);

    }
    render(){
        return (
            <div>
                <h1>Hello Ashkan</h1>
            </div>
        );
    }
}


export default App;

index.js

import ReactDOM from 'react-dom';
import App from './app'



ReactDOM.render(<App/>,document.getElementById('root'));

我收到了这个错误

./src/index.js 第 6 行:使用 JSX react/react-in-jsx-scope 时,'React' 必须在范围内

标签: javascriptreactjs

解决方案


您在这里使用 JSX ( <App />):

ReactDOM.render(<App/>,document.getElementById('root'));

每当你使用 JSX 时,React 都应该在范围内。


推荐阅读