Class and Function Components
Tags: javascript, react, webdev, programming
Published: 2022-06-24 12:53:50

## > **Every drop makes an ocean** :star2: :ocean:

You cannot deny the above quote. Right?

PS: The every blog of this [React Series](https://dev.to/naweli_verma/series), is also more like a water drops, to make you a ocean of knowledge. :sun_with_face:

Well we are going to learn about the most important and a very crucial part of ReactJs, which are **Components**. :volcano:

If you ask me **what are components** by giving a real world example, I will say a **ReactJs web app is an ocean and the components are the water droplets.** 

Makes sense? :smile:

A little, right? :joy:

Be with me, I will tell you what exactly the thing is.

_Here we go now, hold my hand tight, we are going for the ride._ :surfer:

## What are Components :mushroom:

- Components are the small building blocks of any ReactJs app. Every ReactJs web app is made up of components. Either one, two components or thousands of them. It is up to the developer, how they want.

### _You will know it once you try it._ 

- It describes the part of user-interface.

- It is to break down complex UI in small chunks.

- It is reusable and can be nested inside other components. 


![React Components](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cta4wgi7h5gw1r1otq49.jpg)


**Let me give you an example so you can picturize it right** :massage:

Suppose you want a beautiful landing page in your website. What are the things that you will need there?

**Think and think** :bow::bow:

You may want a Header, Footer, Sidebar, Content Body. Integrating all this together you will have your Landing Page. True?

Now this is what component is, small chunks of of your desired page. :rocket:

If you want a Button and you want the same exact button in your website, so you can simply create a Button Component and can reuse wherever you like. 
If you want the same button with different text you can change the text through props. (We will cover this in coming series)


**Now I am pretty sure you know what exactly a Component is, on that victory let's know little more about it** :rocket::rocket::rocket:


## **Types of Components** :mushroom:

There are two types of components:

**- Class Components**

**- Function Components**

## Before ReactJs version 16.8.0** :arrow_left:

## Class Components :mushrooms:


![Class Component](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dd5w5w9vaydbiswobtjn.png)

- It takes object of props optionally and return JSX to the UI.
##### (Don't worry if this line doesn't makes sense a little or at all. You will know it soon it coming series.)

- Class Components are **used** ( _pay attention on "used" word_ )to be called as **stateful** components.

- Capable for handling complex UIs.

- Maintain their own private data or state to give the power of dynamic nature.

- Provide lifecycle hooks

- **this** keyword can be used here.

## Function Components :mushrooms:

![Function Component](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x9e5n91e1eagg34f65lv.png)
- Same like class, it takes object of props optionally and return JSX to the UI.
##### (As I told you earlier, don't worry if this line doesn't makes sense a little or at all. You will know it soon it coming series.)

- It is simple JavaScript function.

-  Function Components are **used** ( _pay attention on "used" word_ ) to be called as **stateless** components.

- No vision for **this** keyword.

- It is not capable of handling any data fetching, other side effects, state rendering or anything which is complex.

##### As you just got to know that functional components never was in the choice list when handling complex operations. :unamused::unamused:


**But Class Components was also not very developer loving, they are complex, specially the "this" keyword. :unamused:

So, to solve this problem, the super genius React parents introduced a super weapon to the react developer, and that is **HOOKS** :heart::heart:

**Devs, things are going to be pretty much more in our favor from here** :dancer::dancer:

## After ReactJs version 16.8.0** :arrow_right:

In this version of React, they introduced a beautiful and a powerful concepts of Hooks to the community. :tiger::tiger::tiger:

**Happy days are back** :smile::smile:

Let me give you a little insights of it.

## Hooks :mushrooms:

I will just give you a little idea about it know.

Hooks gives a developer the power to use all those side effects, complex UI logics in the functional components, without worrying about **this** keyword and complex syntax like class components.

**So from now on, Functional Components also are "Stateful" components** and we will use this in our whole series. :heart_eyes: :smile: :smiley:

**Hurrayyyyy!!!!! ** :heart_eyes::heart_eyes:

So now you know why Hooks were introduced.

If you want to know more [why and what about hooks](https://stackoverflow.blog/2021/10/20/why-hooks-are-the-best-thing-to-happen-to-react/), this will help. :fire:

Okay developers, see you in the next one. 

You can connect with me on [twitter](https://twitter.com/naweli_verma) :smiley:

We will find a way through the dark :fire: