当我想创建一个没有 JSX 的元素时,它向我显示此错误 'React' is not Defined no-undef
。我是初学者。我不知道出了什么问题。谁能帮忙
import logo from './logo.svg';
import './App.css';
const app = React.createElement("h1", null, "Without JSX"); // Showing an error
function App() {
return (
<div className="App">
{app};
</div>
);
}
export default App;
这些消息显示在 VS 代码终端中。
Failed to compile.
./src/index.js
SyntaxError: E:\react_js_course\src\index.js: Unexpected token, expected "," (11:2)
Failed to compile.
src\App.js
Line 4:13: 'React' is not defined no-undef
Search for the keywords to learn more about each error.
最佳答案
根据您提供的代码,有几点:
您在示例中使用了 JSX:您的
App
组件返回 JSX,因此React
应该在执行此操作的范围内。您正在显式使用 React 方法
createElement
,因此React
应该在此文件中可用。
要解决这两个问题,请在此文件顶部添加 import React from 'react';
。
关于javascript - 第 4 行 :13: 'React' is not defined no-undef,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67798320/