3

I just finish typing npx create react app to create a react app but for some reason my App.js is a function not a class eg)

result:

function App() {

  return ()

I want something like:

Class app extends components{
   render (return())
}

I could write it down manually but i want the class app as default when i create an app so could anyone tell me what is the cause of this?

3 Answers 3

6

That just comes out of the box now for React. You can make your App.js a stateless functional component OR a class component.

class App extends React.Component{
   render(){
      return(
        <div>Hello World</div>
      )
   }
}
Sign up to request clarification or add additional context in comments.

2 Comments

That's correct though but why default is Functional instead of Component? While it used to be class before.
@FaizanMubasher Facebook found that class Components were not very reusable, they were lacking the sense of interchangeability that was expected during their conceptualization. React developers would just throw everything in state, and that ended up just creating giant, unrecyclable components. The idea of hooks is that you can extrapolate state outside of the component and integrate them where needed. That is the change that Facebook wants to see with React moving forward.
4

With the introduction of hooks in React 16.8 functional components can be stateful and supposed to replace class components in most cases. This is the reason why App is functional component in a project that is generated with create-react-app, more specifically react-scripts package.

It's possible to initialize the project with older react-scripts as a template:

create-react-app foo --scripts-version react-scripts@^2

And then update it to latest version:

npm -S react-scripts@^3

Since create-react-app provides static boilerplate for a project, and doesn't have scaffolding features, it's possible to copy and paste App from previous react-scripts version:

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;

Comments

0

As mentioned by @FaizanMubasher, the class Components were not very reusable, and they were lacking a sense of interchangeability. It makes no sense to copy class component code from previous versions. It will be ideal to use move away from class components and start using function components. here is an example for anyone seeking the right approach.

import React from 'react';
import Layout from './components/Layout/Layout';

function App() {
  return (
    <div className="App">
      <Layout />
    </div>
  );
}

export default App;

function component Layout:

import React from 'react';
import Aux from '../../hoc/Aux';

const layout = (props) => (
  <Aux>
  <div>TOOLBAR, SIDEDRAWER, BACKDROP</div>
  <main>
    {props.children}
  </main>
  </Aux>
);

export default layout;

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.