DEV Community

Cover image for Building Custom React Hooks
Adrian Bece for PROTOTYP

Posted on • Edited on • Originally published at blog.prototyp.digital

Building Custom React Hooks

React hooks simplify the process of creating reusable, clean and versatile code, and advanced optimization techniques like memoization are now more accessible and easier to use. React’s official documentation doesn’t cover custom hooks in detail as it covers the basic hooks, so the focus of this article is going to be primarily on building custom React hooks and best practices.

Understanding basic React hooks are required to get the most out of this article. If you aren’t already familiar with the basics, there are numerous great articles out there covering them. For example, React’s official docs are a great place to start.

Mindset

In order to build a versatile, performant and reusable custom hook, there are several things to keep in mind.

Hooks run each time component re-renders

Since we’re working with functional components and hooks, we no longer have the need for lifecycle methods. Each time state or a prop is changed, the functional component is being re-rendered and, thusly, our custom hook is being called over and over again.

Use basic hooks as much as possible

Basic React hooks are the core of any custom hook. We can use memoization and hook dependency arrays to control which parts of our custom hook will change or will not change with each re-render. It’s important to understand the role each basic hook can have in our custom hook in order to use them effectively and build performant hooks.

Rules of hooks

There are a few important rules to keep in mind. These rules are explained in detail on React hooks documentation.

Building the custom hook

Now that we have covered the basics, we’re ready to build our own custom hook. In the following example, we’ll establish a solid pattern for building custom hooks and go through some of the best practices.

Let’s imagine that we’re working on a project where users can play multiple games that use dice rolls as part of their game mechanic. Some games require only a single dice to play and some games may require multiple dice to play. We’ll also assume that during some games, the number of dice used may change.

Keeping that in mind, we are going to build useGameDice hook with the following features:

  • Custom hook can be initialized with the number of dice being used and an initial value
  • Function that sets the number of dice being used
  • Function that rolls the dice. Returns an array of random numbers between 1 and 6. Length is determined by the number of dice being used
  • Function that resets all dice values to initial value

Setting up the hook (imports and hook function)

We are declaring our custom hook as a regular arrow function using the recommended convention of naming custom hooks - the name should start with "use" keyword. We are also importing React hooks that we’ll use later on in our implementation. We could also import constants, other functions, other custom hooks, etc.

Our hook can be initialized with 2 optional variables:

  • initialNumberOfDice - how many dice will be used
  • initialDiceValue - determines the initial value and value after reset

Both variables have a default value of 1 to avoid any errors and simplify the hook setup.

import { useState, useMemo, useCallback, useEffect } from "react";

export const useGameDice = (initialNumberOfDice = 1, initialDiceValue = 1) => {
 /* We'll be adding code here in order */
};
Enter fullscreen mode Exit fullscreen mode

Adding state and memoized private variables

First, we need to set up our state. We’ll declare two simple states:

  • diceValue - array which size is defined by numberOfDice and holds value for each dice
  • numberOfDice - determines the number of dice (diceValue array size) that will be used

We are also initializing initialDiceState variable that creates the initial array value that will be assigned on initial render and state reset. This value is memoized to avoid array being initialized and filled with default values on each re-render.

 const [diceValue, setDiceValue] = useState();
 const [numberOfDice, setNumberOfDice] = useState(initialNumberOfDice);

 const initalDiceState = useMemo(
   () => Array(numberOfDice).fill(initialDiceValue),
   [numberOfDice, initialDiceValue]
 );
Enter fullscreen mode Exit fullscreen mode

Adding memoized hook functions

Next, we’ll create the following functions:

  • generateRandomDiceNumber - generates a random number between 1 and 6 (a single dice roll)
  • rollDice - calls a random number generator for each element in the array (dice)
  • resetDice - resets the dice value state to an initial value
const generateRandomDiceNumber = useCallback(() => {
   return Math.floor(Math.random() * 6) + 1;
}, []);

const rollDice = useCallback(() => {
   const arrayConfig = { length: numberOfDice };
   const newDiceValues = Array.from(arrayConfig, generateRandomDiceNumber);
   setDiceValue(newDiceValues);
}, [numberOfDice, generateRandomDiceNumber]);

const resetDice = useCallback(() => {
   setDiceValue(initalDiceState);
}, [initalDiceState]);
Enter fullscreen mode Exit fullscreen mode

We are using useCallback hook to control when the functions are going to be re-initialized. Functions are re-initialized only when any variable in their dependency array changes. In case of the generateRandomDiceNumber function, it’s never re-initialized after the first render and initialization because this function doesn’t depend on any external variable or state.

Adding side effects - hook initialization & update

We need to set up a listener that will watch for updates to our initial dice state. This side effect has two responsibilities:

  1. It sets the dice state to the initial value when the hook is first initialized
  2. It updates the dice state to the initial value when dice number (array size) has changed
 useEffect(() => {
   setDiceValue(initalDiceState);
 }, [initalDiceState]);
Enter fullscreen mode Exit fullscreen mode

API setup & return statement

Finally, we are defining our state and api objects and returning them in an array, following the useState convention. Let’s take a look at each object:

  • state - holds all our state values. We expect this object to change on almost every re-render
  • api - holds all functions. We are returning some of our functions declared in useCallback and a function from useState hook. This object is memoized because we do not expect this to change on almost every re-render
const state = {
   diceValue,
   numberOfDice
 };

const api = useMemo(
   () => ({
     setNumberOfDice,
     rollDice,
     resetDice
   }),
   [setNumberOfDice, rollDice, resetDice]
 );

 return [state, api];
Enter fullscreen mode Exit fullscreen mode

We are returning the objects in an array because we want this hook to be flexible. By doing so, we allow developers to rename the returned variables and allow them to initialize multiple instances of this hook if needed.

 const [diceFirst_state, diceFirst_api] = useGameDice();
 const [diceSecond_state, diceSecond_api] = useGameDice();
Enter fullscreen mode Exit fullscreen mode

Git repository & demo

You can see the final implementation and full code with a demo on the following GitHub repository.

Alt Text

React custom hooks pattern overview

By now, you might have noticed that we grouped the code we were adding in sections. This structured and clean pattern follows a logical path:

  1. State initialization (useState, useReducer), local variables initialization (useMemo), ref initialization (useRef) & external custom hooks initialization
  2. Memoized functions (useCallback)
  3. Side effects (useEffect)
  4. API setup (state and memoized api)
  5. Return statement

Conclusion

It’s no surprise that hooks were well received by React community. Developers are able to share logic between components more easily, create multiple components (interfaces) for each custom hook, pick and choose the parts of hook’s state and API they’ll use in their components, etc.

This reusability and versatility make hooks a real game-changer in React app development. With an established pattern and best practices when building custom React hooks, developers are able to deliver code with consistent quality, clear structure, and optimal performance.


These articles are fueled by coffee. So if you enjoy my work and found it useful, consider buying me a coffee! I would really appreciate it.

Buy Me A Coffee

Thank you for taking the time to read this post. If you've found this useful, please give it a ❤️ or 🦄, share and comment.

This article is also available on Medium, so feel free to give it an 👏 if you've enjoyed it.

Top comments (2)

Collapse
 
mungojam profile image
Mark Adamson

thanks for the neat example. I wonder if you should be wrapping the state creation at the end in a useMemo. Otherwise components that use this hook and render for a reason unrelated to a dice roll will pick up a 'new' dice state and then end up re-rendering some other aspects unnecessarily, e.g. if there is some useMemo call in the calling component with the dice state as one of its inputs.

Collapse
 
scriptkavi profile image
ScriptKavi

Why create one when you can get all awesome hooks in a single library?

Try scriptkavi/hooks. Copy paste style and easy to integrate with its own CLI