React.js / React Hooks

Creating and Using Custom Hooks

This tutorial will teach you how to create and use Custom Hooks. You will learn how to extract component logic into reusable functions and share behaviors between components.

Tutorial 5 of 5 5 resources in this section

Section overview

5 resources

Introduces React hooks for functional components to manage state and side effects.

Creating and Using Custom Hooks

1. Introduction

The goal of this tutorial is to teach you how to create and use Custom Hooks in React. By the end of this tutorial, you will be able to extract component logic into reusable functions and share behaviors between different components.

Prerequisites

To fully grasp this tutorial, you need to have a basic understanding of React and JavaScript syntax, including ES6 features like arrow functions, and the use of state and effect hooks in React.

2. Step-by-Step Guide

A custom Hook is essentially a JavaScript function, but its name starts with 'use', and it can call other Hooks.

Why Custom Hooks

Custom Hooks allow us to reuse stateful logic between components. Before Hooks were introduced, this was done via render props and higher-order components. But custom hooks provide a much easier way to share logic between components.

Creating a Custom Hook

A custom Hook is a normal function which we've decided to call a Hook. It's a convention that our function name should begin with the word 'use'. If the function does call other Hooks, we must follow the rules of Hooks.

Let's create a custom Hook called useWindowWidth that will return the window's width.

import { useState, useEffect } from 'react';

function useWindowWidth() {
  const [width, setWidth] = useState(window.innerWidth);

  useEffect(() => {
    const handleResize = () => setWidth(window.innerWidth);
    window.addEventListener('resize', handleResize);
    return () => {
      window.removeEventListener('resize', handleResize);
    };
  }, []);

  return width;
}

In the above code, we've created a custom Hook that uses the useState and useEffect Hooks to track and update the window width.

3. Code Examples

Using a Custom Hook

Let's see how we can use the useWindowWidth hook we created above in a component.

import React from 'react';
import useWindowWidth from './useWindowWidth';

function Component() {
  const width = useWindowWidth();

  return (
    <div>
      <p>Window width is: {width}</p>
    </div>
  );
}

export default Component;

In the code above, we imported our custom Hook and used it inside our functional component. The component will now always display the current window width, and update it whenever the window is resized.

4. Summary

In this tutorial, we've learned how to create and use Custom Hooks in React. We saw that Custom Hooks are a built-in feature in React that allows us to extract component logic into reusable functions. We also learned how to use the custom Hook in a React component.

Now that you understand Custom Hooks, a good next step would be to learn more about the other types of Hooks that React provides, like useContext, useReducer, and useMemo.

5. Practice Exercises

  1. Create a custom Hook useDocumentTitle that sets the document title to the given input. Use it in a component to set the document title.

  2. Create a custom Hook useLocalStorage that syncs a value with local storage. The Hook should return the current value and a function to update it.

Solutions:

  1. useDocumentTitle Hook:
import { useEffect } from 'react';

function useDocumentTitle(title) {
  useEffect(() => {
    document.title = title;
  }, [title]);
}

Component usage:

import React from 'react';
import useDocumentTitle from './useDocumentTitle';

function Component() {
  useDocumentTitle('My new title');

  return (
    <div>
      <p>Check the document title!</p>
    </div>
  );
}

export default Component;
  1. useLocalStorage Hook:
import { useState, useEffect } from 'react';

function useLocalStorage(key, initialValue) {
  const [storedValue, setStoredValue] = useState(() => {
    try {
      const item = window.localStorage.getItem(key);
      return item ? JSON.parse(item) : initialValue;
    } catch (error) {
      return initialValue;
    }
  });

  const setValue = value => {
    try {
      setStoredValue(value);
      window.localStorage.setItem(key, JSON.stringify(value));
    } catch (error) {
      console.log(error);
    }
  };

  return [storedValue, setValue];
}

Component usage:

import React from 'react';
import useLocalStorage from './useLocalStorage';

function Component() {
  const [name, setName] = useLocalStorage('name', 'Anonymous');

  return (
    <div>
      <input 
        type="text" 
        value={name} 
        onChange={e => setName(e.target.value)} 
      />
    </div>
  );
}

export default Component;

Make sure to practice creating your own custom Hooks and using them in different components to get a good grasp of this concept. Happy coding!

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Fake User Profile Generator

Generate fake user profiles with names, emails, and more.

Use tool

Random Name Generator

Generate realistic names with customizable options.

Use tool

Interest/EMI Calculator

Calculate interest and EMI for loans and investments.

Use tool

Robots.txt Generator

Create robots.txt for better SEO management.

Use tool

CSV to JSON Converter

Convert CSV files to JSON format and vice versa.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help