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

How to Add a Custom Tab to the Customer Order Detail Page in Magento 2

Spread the love Adding a custom tab to the Order Detail

Exception Handling in Java

Spread the love Introduction – Exception:An unwanted

How to add custom less file in Magento2

Spread the love Source files *.css included into layout

Spring Boot Microservices

Spread the love Microservices architecture has become

Implementation of WireMock for better testing

Spread the love WireMock is a flexible and powerful tool

Magento 2: How to selected default option in configurable options?

Spread the love Configurable products in Magento 2 allow

How To Implement Simple JWT Authentication in Django Rest Framework

Spread the love We’ll learn how to use simple JWT

Optimizing Search: Integrating Elasticsearch in Django Applications

Spread the love What is Elasticsearch? Elasticsearch is a

How to create Customer Segments

Spread the love In Magento 2, “customer

Proxy Design Pattern and Requisition List in Magento 2

Spread the love Requisition List in Magento 2 Requisition

bluethinkinc Blogs