Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 297
Nadav Shabtai Avatar asked May 24 '19 18:05

Nadav Shabtai


1 Answers

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; 
like image 76
Richard Avatar answered Oct 04 '22 23:10

Richard