简体   繁体   English

第 4:13 行:'React' 未定义 no-undef

[英]Line 4:13: 'React' is not defined no-undef

When i want to create an element without JSX it shows me this error 'React' is not defined no-undef .当我想创建一个没有 JSX 的元素时,它会显示这个错误'React' is not defined no-undef I am a beginner.我是初学者。 I don't know what is the problem.我不知道是什么问题。 Can anyone help谁能帮忙

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;

These messages are shown in VS code Terminal.这些消息显示在 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.

A couple points just based on the code you provided:仅基于您提供的代码的几点:

  1. You are using JSX in your example: your App component returns JSX, so React should be in scope to do that.您在示例中使用 JSX:您的App组件返回 JSX,因此React应该在 scope 中执行此操作。

  2. You are explicitly using the React method createElement so React should be available in this file .您正在显式使用 React 方法createElement ,因此React应该在此文件中可用。

To fix both issues add import React from 'react';要解决这两个问题,请添加import React from 'react'; at the top of this file.在这个文件的顶部。

import React from 'react' is required for writing code in JSX and alternative you use ES7 snippet which help to auto populate the code在 JSX 中编写代码需要 import React from 'react' 并且您可以使用 ES7 片段来帮助自动填充代码

enter image description here在此处输入图像描述

Img-extension ss图片扩展名 ss

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM