Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I configure create-react-app to create an app with classes and not functions?

I am following a YouTube tutorial in order to learn React, but I noticed something annoying. When I create a new app with create-react-app, the app.js is using a function App(), and not a class. I know React can be coded both ways, but I would like to work with classes. Is there any way I can configure create-react-app to create a React app with classes? Something like adding an argument on the CLI, or maybe change an option on a setup page (I couldn't find any).

function App() {}

What I want:

class App extends Component {}
like image 974
Jeremy S Avatar asked Nov 21 '25 14:11

Jeremy S


2 Answers

If you would like your create-react-app to generate class instead of a function then you need to use the following command to create App with the older script version.

create-react-app my-app --scripts-version react-scripts@^2
like image 81
Hassan Avatar answered Nov 24 '25 03:11

Hassan


What you are observing is not adjustable by configuration. They changed the App.js generation in create-react-app from class to function with commit e4fdac2 and commit b424737. You are using a version of create-react-app more recent than the videos you are watching. Either syntax is valid, depending on whether you need state or not, but you will have to alter the code yourself to use the former class syntax in App.js.

like image 34
Phil K Avatar answered Nov 24 '25 03:11

Phil K



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!