function App() vs class App extends Component in the App.js file

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.


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


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.

Tags:

Reactjs