Blog / React

How to use useDebugValue in React

How to use useDebugValue in React

Learn how to use useDebugValue in React as well as what it is, and when to use it.

Will MaygerWill Mayger
January 21, 2021
Article

For the most part, you aren’t going to need to use useDebugValue in your React hooks, but sometimes it can be helpful depending on how complex your custom React hook is.

We are going to go over the hook useDebugValue, what it is, how to use it and then when you should use it.

There will be examples of useDebugValue for each part as well!

As with all of my articles, I will be writing in plain english without technical jargon so that everyone, no matter what experience level can understand and then use this hook.

So let’s get started with useDebugValue!

What is useDebugValue in React?

The hook useDebugValue works hand in hand with the react dev tools from Facebook.

It enables you to be able to log information in the dev tool in an easier format than just seeing the values of the hook.

Here is how the react dev console looks:

React dev tools example

Here is how an example hook might look without the use of the useDebugValue react hook:

React dev tools without useDebugValue example

And here is how our example hook will look in the react dev console after using useDebugValue:

React dev tools with useDebugValue example

In this example the state is pretty simple to start with (Just a single boolean), but even then, using useDebugValue can add some much needed context to the hook.

As your custom hooks grow in complexity, you may find that if you need to debug them, it will be much simpler to be able to just glance at a string like in the photo above that tells you what state the hook is in.

What about console.log?

Personally, my go to for debugging JavaScript and React code is to use console.log, and I would be lying if I said I would use useDebugValue instead of console.log.

Console.log provides a quick, easy, universal way to debug your code with very little work and without having to import any functions or new code to make it work.

With that being said, there are definitely times that you would want to use useDebugValue over console logging which we will go over a little later on.

How to use useDebugValue in your react hook.

Using the react hook useDebugValue is pretty straightforward, you just need to call the function inside of a custom react hook along with a string that will be used to debug with.

Here is a useDebugValue example:

import React, { useDebugValue, useState } from "react"

export default function useExampleHook() {
  const [b, setB] = useState(false)

  useDebugValue("Rocket is Inactive")
  return [b, setB]
}

export default function App() {
  useExampleHook()
  return <div />
}

As you can see, this could have a lot of potential if we use it to highlight the current status of the hook.

To do that we just need to change the string conditionally, or we can call the function conditionally.

In the next example you will see it being used to determine the basic status of the custom hook by conditionally choosing which string to display in react dev tools.

import React, { useDebugValue, useState } from "react"

export default function useExampleHook() {
  const [b, setB] = useState(false)

  useDebugValue(b ? "Rocket is Active" : "Rocket is Inactive")
  return [b, setB]
}

export default function App() {
  useExampleHook()
  return <div />
}

When to use useDebugValue in React?

You should only use this hook in combination with a custom React hook.

Even then, it doesn’t mean you need to add it to each and every custom react hook you make either.

You only really need to add it if you have a complex hook that you may need to re-visit in the future, or if you are making a react hook as part of a library and the hook’s state is not immediately obvious from looking at the values in the hook.

Simply put, use it sparingly because for the most part it won’t be needed.

With that said, it is always a good thing to know how to use it so you can add another tool to your toolbox for the future.

To summarize useDebugValue

In summary, useDebugValue is a hook which will allow you to improve debugging hooks by letting you write a human readable string which will be easier to read and debug than values would be.

I hope you liked this article, please check out some of my others!

Image icon made by Freepik from www.flaticon.com
Foxi - Budget Planner & Tracker

Foxi

Budget Planner & Tracker

More money in your pocket by the end of the month.

Free to use and no account needed.

Get started now.

Get the app

Some graphics used on this post were made using icons from flaticon.

Latest Posts

Learn React, JavaScript and TypeScript

Learn React, JavaScript and TypeScript

Join the platform that top tier companies are using.
Master the skills you need to succeed as a software engineer and take your career to the next level with Pluralsight.

Start here

Become an expert in ReactJS, TypeScript, and JavaScript.

Here you will find my personal recomendations to you, for full disclosure I earn a small commission from some of these links, but I only recommend what I trust and personally use.

Good things are coming, don't miss out!

Good things are coming, don't miss out!

Follow me on Twitter to stay up to date and learn frontend, React, JavaScript, and TypeScript tips and tricks!

Are you a novice, intermediate or expert react engineer?

Find out here by taking my fun, interactive, quick quiz which takes approximately 1 - 3 minutes. How well will you will do?

Foxi - Budget Planner & Tracker

Foxi

Budget Planner & Tracker

More money in your pocket by the end of the month.

Free to use and no account needed.

Get started now.

Get the app