简体   繁体   English

为什么create-react-app在创建新应用时默认不导入App.js中的组件?

[英]Why is create-react-app not importing component by default in App.js when creating a new app?

So my issue is when i do "create-react-app" in terminal it creates everything like it should but in App.js it should be importing React and { Component } from "react" by default no?所以我的问题是,当我在终端中执行“create-react-app”时,它会创建应有的所有内容,但在 App.js 中,它应该默认从“react”导入 React 和 { Component }否?

Because right now it does this:因为现在它这样做:

import React from "react";

And not this:而不是这个:

import React, { Component } from "react";

And also it's not doing: class App extends Component {... } But rather: function App() {... }而且它没有做: class App extends Component {... }而是: function App() {... }

So when i try to use state it just fails to compile saying 'state' is undefined because of course it is if it's not importing Component.因此,当我尝试使用 state 时,它只是无法编译说“状态”未定义,因为它当然是如果它没有导入组件。

Why is that?这是为什么? I have the latest version of Node.js installed.我安装了最新版本的 Node.js。 I really don't want to redo imports everytime i run create-react-app.我真的不想每次运行 create-react-app 时都重做导入。

I'm learning React so maybe i did something wrong with my install or something, i don't know.我正在学习 React,所以也许我在安装时做错了什么,我不知道。 I'm hoping it's something i overlooked and that it's easy to solve, maybe start fresh?我希望这是我忽略的事情并且很容易解决,也许重新开始?

There are two kinds of components, Class components and Functional components.有两种组件,Class 组件和功能组件。 You can use whatever approach you like, either class components that extend the React.Component or functional components that do not extend the React.Component because they are pure functions.您可以使用任何您喜欢的方法,可以是扩展 React.Component 的 class 组件或不扩展 React.Component 的功能组件,因为它们是纯函数。 https://reactjs.org/docs/components-and-props.html https://reactjs.org/docs/components-and-props.html

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

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