Friday, September 11, 2020

Functional vs class component

 Today we will understand difference between functional and class component


The most obvious one 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 which you will see later on.

State

Because a functional component is just a plain JavaScript function, you cannot use setState() in your component. That’s the reason why they also get called functional stateless components. So everytime you see a functional component you can be sure that this particular 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.

No comments:

Post a Comment