How to create custom Hook in React

Blog
Spread the love

In this article, we will learn how to create custom hooks in React. Primarily, Hooks are introduced to implement the functionalities of a Class component into a Functional component. Hooks are reusable functions.

Why do we need to use custom Hooks in React

Custom hooks enable us to remove unnecessary duplicated logic from our React components.

Reusability – The same code can be uses multiple times without writing it many times.

Maintainability – We have to change the logic of hooks only once.

It improves the readability of a React functional component.

Rules to write and implement a custom React hook

Custom hooks begin with keyword ‘use’. It is not necessary to begin the custom Hook name with the use keyword but I recommend you to write
custom Hooks starting with the use keyword because it enables us to spot the bug quickly.

Custom hooks always contain built-in React hooks or other custom hooks. If a custom hook does not contain any hook then it will not be a custom
hook.

We should call Hooks at the top level. We cannot use hooks inside nested functions, conditions and loops.

Now, let’s create our own custom React Hooks.

Firstly, I am going to create simple Counter Functional component in which our state value increase and decrease by 1 and initial value is set to 0.

Output will be shown like this

First.gif

Now, if we want to render above functionality in multiple component so here custom hook comes into the role. Basically we will modify the Counter component to make a hook named as ‘useCounter’. So after some modification code will look like this.

Let’s say, we want to use our custom hook ‘useCounter’ in three different component e.g. Counter1.jsx, Counter2.jsx, Counter3.jsx. So we will import in our components like this.

Counter1.jsx

Counter2.jsx

Counter3.jsx

You can see that initial state value of every component is changed.

Now we have to import all these components in our App.js file to render it on the webpage.

Output will be shown like this

Second_gif

Now you are able to create your own custom React hook.

CONCLUSION

I hope this blog help you in an effective way. Please give your valuable suggestions in the comment section below.

Thanks for reading.

Have a nice day !

bluethinkinc_blog

bluethinkinc_blog

2023-04-05

0

Leave a Reply

Your email address will not be published. Required fields are marked *

Find More Blogs

Proxy Design Pattern and Requisition List in Magento 2

Spread the love Requisition List in Magento 2 Requisition

Google Ads Conversion Tracking Through Magento Platform:

Spread the love Google Ads Conversion Tracking Through

Object Pool Pattern in Magento2

Spread the love What is Object Pool Pattern in Magento 2 An

JENKINS

Spread the love JENKINS Introduction and Installation

Virtual Type and Type

Spread the love Virtual type is one of the most common

Customize the length of Input field

Spread the love Sometimes we need to change the limit the

How to Add Custom Tab on Admin Dashboard and Count Website Total Visitor in Magento 2?

Spread the love In this blog we’ll see How to Add Custom

How to Add Sign Out Tab in Customer Account in Magento 2?

Spread the love In this blog we’ll see How to Add

How to create custom tab in product detail page in magento 2?

Spread the love In this blog we’ll see how to add

Understanding and Implementing CORS & CSRF

Spread the love What is security? The world has become more

bluethinkinc Blogs