43

I'm following a tutorial on React (https://www.youtube.com/watch?v=sBws8MSXN7A - dated January 3, 2019) and created a React app with npx create-react-app *app_name*. The App.js file this command generated on my computer is different from what this command generated for the person giving the tutorial. Has React changed since then or is it possible I downloaded something wrong?

My App.js:

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;

Tutorial's App.js:

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

    class App extends Component { //different
      render() ( //different
        // The rest of the file is the same
        <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;
2
  • 5
    CRA now generates a functional component by default, as the component has no state. See reactjs.org/docs/components-and-props.html - you can write either. Commented Jun 19, 2019 at 22:01
  • 1
    So basically if your component has no state just use functional and if it has state you MUST use class? Commented Jan 20, 2021 at 20:01

4 Answers 4

21

The most obvious difference is the syntax. A functional component is just a plain JavaScript function which accepts props as an argument and returns a React element.

A class component requires you to extend from React.Component and create a render function which returns a React element. This requires more code but will also give you some benefits.

A functional component doesn’t have its own state. If you need a state in your component you will either need to create a class component or you lift the state up to the parent component and pass it down the functional component via props.

Another feature which you cannot use in functional components are lifecycle hooks. The reason is the same as for state, all lifecycle hooks are coming from the React.Component which you extend from in class components. So if you need lifecycle hooks you should probably use a class component.

Conversely, functional components allowed to use hooks where class components are not allowed to.

Sign up to request clarification or add additional context in comments.

2 Comments

"A functional component doesn’t have its own state." Is this statement still true regarding the use of state hooks in functional components?
@ᐅdevrimbaris No
19

There are basically two ways of writing components in React: functional and class components. The given examples are no different except for this aspect.

React has recently updated their documentation website and switched all examples from class components to function components. You can check it out https://react.dev/learn

4 Comments

Can you have state in the functional component? Or is all functional component stateless
Now since react introduced hooks, it's possible to have state in functional components as well. Please refer to reactjs.org/docs/hooks-state.html
Then why does adding const [x, y] = useState(''); to class App extends Componentgive an error on the equals sign ; expected ? they are clearly different and the same code creates an error on one.
Using hooks is of course not supported in class components. But the notion of "component" remains the same. There are just two different ways of writing a component, each with their own rules.
12

There is one major difference between these methods of defining a component. It's related to the state of the particular React component.

So, Function-Based Components => Are also called Stateless components reason being they don't update to any changes that are being applied to a particular component.

So, If I want to change some value in this <p>Hi, Welcome</p> on a button press to <p>Welcome Back</p> It wouldn't be possible to use function-based components.

On the other hand, Class-Based Components => Are also called Stateful Components, being that they update to changes that occur to the component.

So, the previous example would work.

Personally, I would say a simple way to remember is Functional Components for static data and Class-Based Components for dynamic and interactive data.

Comments

2

The obvious differences are in the syntax and hints are derived from their names in how they will be written. They both render JSX (which is React’s way of writing HTML and JavaScript), however for the class component you define a class that extends the React.Component library. Additionally, the class component utilizes a render() method. Here’s an example:

App.js: functional component

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

function App() {
    return (
        <h1>hello</h1>
    );
}

export default App;

App.js: class component

import React, { Component } from 'react';

class App extends Component {
    render() {
        return (
            <h1>hello</h1>
        );
    }
}

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.