Nadav Shabtai
Nadav Shabtai

Reputation: 687

create-react-app generates function instead of class in App.js

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?

Upvotes: 25

Views: 18516

Answers (6)

Thomas M
Thomas M

Reputation: 339

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 . 

Upvotes: 2

F&#225;bio BC Souza
F&#225;bio BC Souza

Reputation: 1220

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

Upvotes: 5

Mohmud-Dayib
Mohmud-Dayib

Reputation: 9

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;


Upvotes: 0

Chad
Chad

Reputation: 618

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.

Upvotes: 12

Richard
Richard

Reputation: 487

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;

Upvotes: 22

Brian Gerhards
Brian Gerhards

Reputation: 849

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

Upvotes: 3

Related Questions