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

Customize lead qualification for a seamless experience

Spread the love When the 2024 Release Wave 2 was launched,

Create a Custom Console Command in Spryker

Spread the love In Spryker, developers can create custom

SALESFORCE

Spread the love Salesforce is a cloud-based software

CRUD Operation in Spryker Backoffice with a Custom Module

Spread the love Spryker is a highly flexible and modular

How to create custom module on Spryker for Backoffice?

Spread the love Creating a custom module in Spryker

How to Install Spryker Project with Docker on Local Machine

Spread the love Spryker is a flexible and scalable commerce

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

bluethinkinc Blogs