ReactJS

React Custom Hooks with Examples

Hello ReactJS Friends,

Today we will learn about the ReactJS Custom Hooks. What are they? And How to Create Custom Hooks in React?

ReactJS added hooks since the release of version 16.8. Using hooks, the function components can access state and other React features. React provides built-in hooks: useEffect, useState, useContext, useRef, useMemo, useCallback and more. Every hook has its own use and properties. Using these hooks as a base, ReactJS Developers can create their own custom hooks.

What are Custom Hooks in React?

Custom Hooks are reusable functions. Custom React hooks are an essential tool that lets you add exceptional, unique functionality to your React applications. Using custom hooks, you can fulfill missing functionalities of your React projects.

Rules for using Custom Hooks in React

  • As a function, it takes input and returns the output.
  • Its name starts with “use” like useQuery, useMedia, etc.
  • Unlike functional components, custom hooks return normal, non-jsx data.
  • Unlike normal functions, custom hooks can use other built-in hooks such as useState, useRef… and even other custom hooks.

Benefits of using Custom React Hooks

Custom hooks in React offer several advantages:

  • Reusable – React custom hooks can be used repeatedly, reducing the time for writing the code twice.
  • Less Complexity – Custom hooks provide a cleaner codebase that helps to reduce complexity and redundancy.
  • Limitless hooks – Following the ReactJS hook standards, you can create any number of custom hooks as per your requirement. Also, lots of custom hooks are available on the internet, which you can use.

Example of ReactJS Custom Hook:

import { useState, useEffect } from "react";
import axios from "axios";
export const useAxiosFetch = (axiosParams) => {
    const [data, setData] = useState(undefined);
    const [error, setError] = useState("");
    const [loading, setLoading] = useState(true);
    const fetchData = async () => {
        try
        {
            const response = await axios.request(axiosParams);
            setData(response.data);
        } catch (error) {
            setError(error);
            setLoading(false);
           } finally {
               setLoading(false);
             }
   };
   useEffect(() => {
       fetchData();
   }, []);
   return { data, error, loading, fetchData };
};

Conclusion:

This way, you can easily create custom hooks in React. Share your doubts with me in the comment box. Share the tutorial with your friends and stay updated to learn more about ReactJS.

Happy Coding!

 

Click to rate this post!
[Total: 3 Average: 5]
Bharat Desai

Bharat Desai is a Co-Founder at MageComp. He is an Adobe Magento Certified Frontend Developer 🏅 with having 8+ Years of experience and has developed 150+ Magento 2 Products with MageComp. He has an unquenchable thirst to learn new things. On off days you can find him playing the game of Chess ♟️ or Cricket 🏏.

Recent Posts

Mastering Tailwind CSS in Laravel: A Comprehensive Guide

Tailwind CSS has emerged as a powerful utility-first CSS framework, offering developers a unique approach…

2 days ago

React Native or Flutter in 2024

The mobile app development field has witnessed a rapid revolution over the past few years.…

4 days ago

Magento 2: How To Call JS on the Checkout Page?

Hello Magento mates, Today we will learn to add a call JS on the checkout…

1 week ago

Boost Your SEM Game: Unveiling the Top 10 Tools for Marketers in 2024

Business survival in today’s digital world has become extremely difficult. Using traditional marketing techniques is…

1 week ago

Five Essential Payroll Compliance Tips for eCommerce Startups

Are you setting up a payroll system for your eCommerce startup? Ensuring compliance with myriad…

1 week ago

Optimizing Laravel Blade: Unlocking Advanced Fetcher Techniques

In the expansive universe of Laravel development, Blade serves as the stellar templating engine, propelling…

1 week ago