🪝 Introduction to useDebugValue
The useDebugValue Hook is a React Hook that lets you display custom debugging information for your own custom Hooks in React Developer Tools. It does not affect the user interface or application behavior, but it helps developers understand the internal state of custom Hooks while debugging.
Important
🎯 Why Use useDebugValue?
When building reusable custom Hooks, it can be difficult to understand their current state while debugging. useDebugValue allows you to provide meaningful labels that appear in React Developer Tools, making custom Hooks easier to inspect and debug.
- Display custom information in React Developer Tools.
- Improve debugging of custom Hooks.
- Make reusable Hooks easier to understand.
- Provide descriptive debugging labels.
⚙️ Syntax
Basic Syntax
useDebugValue(value);
useDebugValue(value, format);| Parameter | Description |
|---|---|
| value | The value displayed in React Developer Tools. |
| format | An optional formatting function that transforms the displayed value. |
🔄 How useDebugValue Works
💻 Example 1: Basic Usage
Simple Debug Value
import { useDebugValue, useState } from "react";
function useCounter() {
const [count, setCount] = useState(0);
useDebugValue(count);
return {
count,
increment: () => setCount(c => c + 1)
};
}When inspecting the custom Hook in React Developer Tools, the current counter value is displayed as debug information.
💻 Example 2: Formatting the Debug Value
Formatted Debug Value
import { useDebugValue } from "react";
function useOnlineStatus(isOnline) {
useDebugValue(
isOnline,
status => status ? "Online" : "Offline"
);
return isOnline;
}The formatting function converts the raw value into a more readable label that appears in React Developer Tools.
💻 Example 3: Custom Hook with Status
Displaying Hook State
import {
useDebugValue,
useState
} from "react";
function useLoading() {
const [loading] = useState(false);
useDebugValue(
loading,
value => value ? "Loading" : "Ready"
);
return loading;
}Instead of displaying true or false, React Developer Tools displays descriptive text that is easier to understand.
📊 Without vs With useDebugValue
| Without useDebugValue | With useDebugValue |
|---|---|
| Limited debugging information. | Custom Hook values appear in React Developer Tools. |
| Harder to inspect custom Hooks. | Hook state is easier to understand. |
| No descriptive labels. | Meaningful debug information is displayed. |
📅 Debugging Lifecycle
The custom Hook executes.
useDebugValue() registers a debug value.
React Developer Tools reads the registered value.
Developers inspect the custom Hook during debugging.
The application behavior remains unchanged.
🎯 Common Use Cases
Display the internal state of reusable custom Hooks.
Show descriptive labels such as "Loading", "Connected", or "Offline".
Format complex values into developer-friendly output.
Improve debugging support in shared Hook libraries.
⚠️ Common Mistakes
- Using useDebugValue in ordinary components instead of custom Hooks.
- Expecting debug values to appear in the application UI.
- Adding unnecessary debug values to every Hook.
- Displaying overly complex or difficult-to-read information.
Warning
📊 useDebugValue vs console.log()
| Feature | useDebugValue | console.log() |
|---|---|---|
| Purpose | Display Hook information in React Developer Tools. | Print messages in the browser console. |
| Visible To | React Developer Tools users. | Anyone viewing the browser console. |
| Typical Usage | Custom Hook debugging. | General debugging and logging. |
| Affects UI | ❌ No. | ❌ No. |
✅ Best Practices
- Use useDebugValue only inside custom Hooks.
- Provide short, meaningful debug labels.
- Use the formatting function for complex values.
- Avoid adding debug values to every Hook unless they improve debugging.
- Remember that this Hook is a developer tool, not a user-facing feature.
📚 Official Resource
Learn more about useDebugValue in the official React documentation at React useDebugValue Documentation.