[英]`npm init react-app` leads to `'React' is not defined no-undef` when changing function component to class component
I used npm init react-app appname
which creates, among other files, App.js
.我使用
npm init react-app appname
创建App.js
等文件。 In that file is a function component:在该文件中有一个 function 组件:
function App() {
return (
<SomeJSX />
);
}
I edited the function component into a class component, like so:我将 function 组件编辑为 class 组件,如下所示:
class App extends React.Component{
render() {
return (
<TheSameJSX />
);
}
}
Now, when I run npm start
, I get an error:现在,当我运行
npm start
,出现错误:
Failed to compile
src/App.js
Line 4:19: 'React' is not defined no-undef
Search for the keywords to learn more about each error.
I imagine I need to add some setting somewhere that will automatically include React without me needing to explicitly import
it at the top of every file.我想我需要在某个地方添加一些设置,这些设置将自动包含 React,而无需在每个文件的顶部显式
import
它。 How do I do this?我该怎么做呢? And why does this npm package not do that by default?
为什么这个 npm package 默认不这样做? I know a bit about javascript (and html and css), and have read a bit about React, but I am completely unaware of how npm or webpack works.
I know a bit about javascript (and html and css), and have read a bit about React, but I am completely unaware of how npm or webpack works.
Thanks in advance!提前致谢!
EDIT: To clarify, I know how to import stuff with javascript.编辑:澄清一下,我知道如何用 javascript 导入东西。 I can easily add
import React from 'react';
我可以轻松添加
import React from 'react';
to the file and make it work.到文件并使其工作。 However, I find it difficult to believe that adding an
import
statement to every single javascript file is the recommended method, and I don't understand why this example app wouldn't be set up so as to avoid having to do that.但是,我很难相信向每个 javascript 文件添加
import
语句是推荐的方法,我不明白为什么不设置此示例应用程序以避免必须这样做。 Am I mistaken?我弄错了吗? Do I really need to manually import the same thing over and over again within the same project?
我真的需要在同一个项目中一遍又一遍地手动导入相同的东西吗? Could I set a global variable to
React
so that I can use it from wherever?我可以为
React
设置一个全局变量,以便我可以在任何地方使用它吗?
In your default function component you're not extending any classes and just writing a simple function在您的默认 function 组件中,您没有扩展任何类,只是编写一个简单的 function
function App() {
return (
<SomeJSX />
);
}
In class component, you're in fact extending the Class Component
by React.Component
provided by React default export object and hence you must import it from the package In class component, you're in fact extending the Class
Component
by React.Component
provided by React default export object and hence you must import it from the package
//only use one of these
import * as React from "react";
import {Component} from "react"; // you can directly extend without writing `React.` with this import
import React from "react"
So your code would be所以你的代码是
import React from "react";
class App extends React.Component{
render() {
return (
<TheSameJSX />
);
}
}
Any of the above imports should be fine with a preference to the first and second one.上述任何进口都应该优先于第一个和第二个进口。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.