Lang
Blog

What are React Functional Components?

ByKhushi Maheshwari
February 16th . 5 min read
What are React Functional Components?

Introduced in May 2013 by Jordan Walke, a Software Engineer at Facebook, at a JavaScript conference, React proved to be a game-changer and became the king of JavaScript libraries in no time.

Before React, all the web applications were made with the help of HTML directives or templates, which was a bit tough. But React made is easier for the developers to build complex applications too as it breaks the whole user interface into several components which when put together completes the UI of the app.

According to Statistics, React was the most used web framework among developers worldwide in 2021 with a total share of 40.14% leaving jQuery behind with 34.43%.

Earlier, the Class component was the main hero of React but after the launching of its new document i.e. React Beta, React Functional Components came into the picture, and we can see that it is the future since there is an introduction of Hooks in version 16.8.

To understand the difference between the two, it is necessary to learn what they both are and what makes Functional components more efficient than class components. So, let’s move-

What are Class Components ?

A component is a piece of code that is responsible for rendering a certain section of the user interface.

Class component is the part of React which uses ES6 JavaScript class to create a component.

class Car extends React.Component {
  render() {
    return <h2>Hi, I am a Car!</h2>;
  }
}

In order to make a class component, you need to create a class that extends React.Component and has a render function. Inside this function, you can return your JavaScript code as usual. It has another name as Stateful component because it implements logic and state.

Every React Component has a lifecycle of its own, which comprises a series of methods that are invoked in different stages of the component’s existence.

Now what do you mean by different stages, what does it refer to? A React component can go through four stages and these are-

  • Initialization: This is the stage of constructing the component with the given Props and default state in the constructor of a Component Class.
  • Mounting: This is the stage of rendering the JSX returned by the render method itself.
  • Updating: This is the stage when the state of a component is updated and the application is repainted.
  • Unmounting: This is the final step of the component lifecycle to remove the component from page.

All component lifecycle methods interact with the component at various stages of its lifecycle. With the use of the Class component, it was difficult to manage the states but with Hooks, it becomes easier to manage.

Let’s see what a Functional component is and how it is becoming the future-

What are Functional Components?

They are nothing but a plain JavaScript function that can accept props as an argument and will return a React element. These functions may or may not receive data as parameters.

The best part about functional components is they do not use any render method. To create a functional component you have to declare a function just as you do in JavaScript.

Initially, functional components were lacking to build more complex apps as they were not having a strong set of features and capabilities but after the advent of Hooks, it is now very convenient for developers to use functional components.

Hooks are special functions that extends the usage of ReactJS features in the functional components.

Functional components do not have access to lifecycle functions like class-based components do, since lifecycle functions need to be defined within the boundaries of a class.

Let’s get a better view of differences between class and functional components-

what-are-react-functional-components – 1.jpg

To use these functions, we can use various kinds of Hooks and the application will work normally.

Hooks in functional components-

The motivation behind the introduction of React Hooks in functional components was to solve the problems “encountered over five years of writing and maintaining tens of thousands of components”.

  • Hooks are useful as they brought the power of the state and lifecycle methods into functional components.
  • They can be easily used in functional components like const [user,SetUser]= React.useState(‘ ‘);

Some names of the updated Hooks in version 16.8 are-

  • useState
  • useEffect
  • useContext
  • useReducer
  • useCallback
  • useMemo
  • useRef
  • useImperativeHandle
  • useLayoutEffect
  • useDebugValue

Out of these all, there are only two hooks that we frequently used, i.e. useState and useEffect. The useState() is used to initialize only one state variable to initialize multiple state variables, multiple useState() declarations are required.

Similarly, we have different uses for all these different hooks. However, the best part is if these hooks are not sufficient then React also gives you the option to build your own custom hooks which makes it more reliable and efficient.

Bottom Line

We have seen how the functional components make complex things easier-to-work, more useful, and are the upcoming future of React applications.

However, considering the class components have their own importance, team React has not yet decided to remove them but evidently considers that the future of React is with the functional components.

Thanks for reading this blog, I hope you got a little better understanding of React functional components than you had before! Feel free to let us know.😊

Share:
0
+0