Manasi Salvi

React - Day three

The question I faced repeatedly was do you set up components as classes or functions. The official docs blog says:

“When a component is defined as a class, it is a little bit more powerful than a function component. It can store some local state and perform custom logic when the corresponding DOM node is created or destroyed.

A function component is less powerful but is simpler, and acts like a class component with just a single render() method. Unless you need features available only in a class, we encourage you to use function components instead.

However, whether functions or classes, fundamentally they are all components to React. They take the props as their input, and return the elements as their output.”

For example - for something as simple as a navbar - which do you go by? Do you set up the navbar as a functional component and have less features or a class component where you leave the door open for addition future features.

Another really good explanation was in this article. The advantage of using a class component is that it has a state and has more features as opposed to functional component which is stateless and ‘read-only’.

For something like a navbar which I don’t expect to change in the near future - I decided to go with function component. However the Home page for this webapp is currently a class component because for now I want to keep the door open for future addition of features.


comments powered by Disqus