The Missing Link in React Debugging: Boost Your React Debugging Skills

Amit Kumar
Frontend2Backend
Published in
2 min readFeb 19, 2024
Photo by Sigmund on Unsplash

Debugging is one of the most important skills for every developer. React Developer Tools is the easiest way to debug websites built with React.

This article answer the below question related to debugging custom hooks in React:

How to debug React hooks like a pro?

Let’s get started with mastering another technique for effective React debugging.

How to debug React hooks like a pro?

Hooks are an important part of React. We use hooks heavily to separate logic from components. Debugging hooks is an important skill to master for every React developer. Debuggers with an IDE and browser DevTools, are not effective for debugging custom hooks.

React developer tools only display information about in-built hooks by default

By default, the debug information in React developer tools is limited to displaying the information about in-built hooks used inside of our custom hook. It creates an issue when debugging a component uses multiple custom hooks.

The useDebugValue hook allow developers to debug custom hooks by assigning custom formatted labels to them. This label can be used to display the current state of a custom hook, making it easier to understand the internal workings of the hook during development.

useDebugValue(value, format?)
  • value: The value that you want to display on the React developer tools.
  • format(optional): The format function formats the value into a readable format. If you don't specify the formatting function then original value itself will be displayed.

Let’s checkout a simple example of useDebugValue hook:

import { useDebugValue } from 'react';

function useItems() {
const items = /* Fetch or compute items... */

useDebugValue(`Items count: ${items.length}`);

return items;
}

With useDebugValue hook in action, React developer tools will start displaying information regarding custom hooks.

Conclusion

Debugging is an essential skill every developer should learn. This simple technique comes in handy when debugging custom hooks in React. It is often very useful during the development process.

--

--

Amit Kumar
Frontend2Backend

Amit Kumar is a frontend developer who love designing web applications with clean, consistent User Interface.