Jeremy S
Jeremy S

Reputation: 47

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 {}

Upvotes: 2

Views: 2903

Answers (3)

Hassan
Hassan

Reputation: 61

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

Upvotes: 6

Shayaan
Shayaan

Reputation: 82

Actually there is a solution to your problem. You just need an older version of react-scripts and your project configuration will fall back to using classes. When creating your project use this command:

create-react-app [Project Name] --scripts-version 2.0.3

Upvotes: 2

Phil K
Phil K

Reputation: 368

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.

Upvotes: 4

Related Questions