简体   繁体   中英

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?

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() {... }

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.

Why is that? I have the latest version of Node.js installed. I really don't want to redo imports everytime i run create-react-app.

I'm learning React so maybe i did something wrong with my install or something, i don't know. 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. 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. https://reactjs.org/docs/components-and-props.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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