Introduction to Functional Components and Class Components
It typically takes developers hundreds of lines of code to develop a single-page application that uses the traditional DOM structure.
As a solution to this issue, a component-based approach gets used if the program requires some changes that are not easily updated. As part of this process, application code gets divided into logical groups known as components.
Functional components in React
While working in React, you will come across some functional components. These are just JavaScript functions. Writing JavaScript functions allows us to create functional components for React. As JavaScript functions, functional components in React look like this:
Our example renders an element representing the Fun user-defined component. In our function component fun, we pass JSX parameter name=” Me” as a prop, returning a <h1>Is anybody living freely, young, and wild? – Me!</h1> element as the output.
Both types of components use props as inputs. The props help pass information from one component to another, and the process is crucial if you want to create a dynamic user interface.
One thing you should keep in mind, however: props are read-only. As a result, all React components should use the same inputs and return the same results for the same props. Properly behaved components get referred to as “pure,” and the classes and functions both fall under that rule.
Class components in React.js
It is the backbone of most modern web applications built in ReactJS. It consists of simple classes (which are composed of multiple functions that add functionality to an application).
Here's an example:
It is an ES6 class that extends the react library's component class. It uses the render() method to return HTML.
As well as working with props, class components can also work with the functional component. The syntax for passing props to components is similar to HTML attributes. To use props, we need to replace props.name in the render() body with this.props.name.
Functional Components vs Class Components
Conclusion
Although It is best to use function components most of the time and React is also allowing the use of state with function components for React version 16.8 and later with the useState hook, as Meta (Facebook App) also recommends. However, in older versions of the react framework, we can use class components that rely on state information