Function And Class Component In React With SPFx
Introduction
In this article, we will learn the different types of components that we can create in React
We will also learn which should be used in which conditions.
React provides us two different types of components:
- Function component
- Class component
React documentation for different components can be found in the below link.
Difference
Function component
- In the syntax point of view, we do not write class for function component rather we create it as a function
- It only accepts props as an argument
- We can not use state with functional component i.e. setState() and hence functional component are also known as stateless components
- In the Functional component, we can not use Lifecycle Hooks.
Class component
- In syntax point of view, we write a class which inherits from React.component
- It accepts both props as well as state
- We can use state and hence it is also known as stateful components
- In class component, we can use Lifecycle Hooks like componentDidMount, the component will unmount
Benefits
Benefits functional component
- As we cannot use lifecycle-hooks and state, it is easy to read and test a functional component
- We end up writing less code and hence it is easy to read and understand
- Performance boosting
- We can use State Hook for storing local data to the component
Benefits class component
- If we want to control the lifecycle of the component we should use class component
- We can store local data in the state which can be used in the component
- It keeps tracks in changes to the data in the state, and reacts if there is any change in it
How to use in SharePoint Framework (SPFx)
For SPFx, let’s create a new solution for SPFx using the below command (Assuming the development environment is set up for SPFx)
|
|
Select different values which are asked while creating the solution, as mentioned below:
Let’s open the code in Visual studio code and create functional and class components in SPFx
Note
By default, the component which is created for the web part is a class component
How to create a functional component in SPFx
Open the file present in the solution in the below path
src -> web parts -> understandComponent -> components
Now to create a functional component, we can add below code in UnderstandComponent.tsx:
|
|
As we can see in the above code, we are not creating a class, rather it is just a function in typescript which has a type as React.FunctionComponent
In this component, we are accepting the title as a parameter and displaying it with tag.
How to create a class component in SPFx
Now to create a class component, we can add below code in UnderstandComponent.tsx:
|
|
From the above code, we can understand that we have created a class named UnderstandStateComponent which is inheriting from React.Component
In this component, we are using the title as property and count as a state and this count would increase on click on the Add button. We can use constructor as this is not a function but a class.
How to use it and render the component
Using function or class component has the same syntax and is very easy to use we can update the render method in our component with the below code:
|
|
As we can see from the above code, we are using both the component in a similar manner and we are passing the props value also in a similar manner hence in terms of component rendering both are similar.
Outcome
Conclusion
Hence, we should use the function component when we want to use it as a presentation component, which does not store any data value but only displays as per the values passed in the property. If we want to store data, we should use class component, which helps us have all lifecycle hooks