25

I'm trying to create react app using create-react-app command, and it's generating the App.js file as function (es5 syntax without class) instead of class (Example in the following code):

import React from 'react';
import logo from './logo.svg';
import './App.css';

function App() {
  return (
    <div className="App">
      <header className="App-header">
        <img src={logo} className="App-logo" alt="logo" />
        <p>
          Edit <code>src/App.js</code> and save to reload.
        </p>
        <a
          className="App-link"
          href="https://reactjs.org"
          target="_blank"
          rel="noopener noreferrer"
        >
          Learn React
        </a>
      </header>
    </div>
  );
}

export default App;

How can I force create-react-app to generate class instead?

8
  • What's your version? create-react-app -V Commented May 24, 2019 at 18:39
  • Hi, thanks for the quick reply, I have version 3.0.1 Commented May 24, 2019 at 18:41
  • Just change it to a class. What is the issue? If you need assistance changing to a class please update your question. Commented May 24, 2019 at 18:42
  • Why does that matter? They're equivalent, and it's relatively easy to switch between them (e.g. IntelliJ/WebStorm can do it for you). Commented May 24, 2019 at 18:42
  • 1
    @NadavShabtai I think you should install react snippets extension for your code editor, so you can create easily classes or other things. Commented May 24, 2019 at 18:47

6 Answers 6

22

you can easily change it to a class like this:

import React, {Component} from 'react';
import logo from './logo.svg';
import './App.css';

class App extends Component {
  render() {
    return (
      <div className="App">
        <header className="App-header">
          <img src={logo} className="App-logo" alt="logo" />
          <p>
            Edit <code>src/App.js</code> and save to reload.
          </p>
          <a
            className="App-link"
            href="https://reactjs.org"
            target="_blank"
            rel="noopener noreferrer"
          >
            Learn React
          </a>
        </header>
      </div>
    );
  }
}
export default App;
Sign up to request clarification or add additional context in comments.

Comments

12

This is the default file when you generate a new project.

Here's the link to the file that's copied over. I believe the older versions of CRA did have classes, but in terms of forcing it to generate files with classes it's currently not set up to do that.

Comments

5

Yes, now days react is much better in functional components, we dont need class components more. You can use Hooks for creating states and so on.

Take a look into the docs:

https://reactjs.org/docs/hooks-overview.html

1 Comment

3

To answer your question, you can't force create-react-app to generate class, but you can use an older version of the package. The template is copied from https://github.com/facebook/create-react-app/blob/master/packages/react-scripts/template/src/App.js into your newly created project, not generated.

A quick item to add, the class was generated in a previous version as an arrow function. They made the change back to what you are seeing in order to keep the Create-React-App consistent with the React documentation github.com/facebook/create-react-app/pull/6655

Comments

2

Yes, you can actually use create-react-app to still generate class based components by default but you have to specify a previous version of the scripts (they changed it to functional components in version 3.0 - see here: https://github.com/facebook/create-react-app/pull/6451)

So this should work:

npx create-react-app my-app --scripts-version react-scripts@^2 . 

2 Comments

Works like a charm! Why on earth is it not so clearly specified in any documentation or course? Your comment should have a lot more thumbs up!
I agree 100% @GiacomoSantarnecchi ! Thanks for your nice words....
0

Don't bother yourself just change it manually.

class App extends Component {
  render() {
    return (
      <div className="App">
        <h1> My first react project</h1>
      </div>
    );
  }
}

export default App;


Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.