
what is useDebugValue ?
useDebugValue() is a React Hook used to label custom hooks in React DevTools — making it easier to debug and understand the hook's state during development.
It's mostly used inside custom hooks to help developers see useful info in React DevTools.
Syntax
useDebugValue(value);
or with formatting:
useDebugValue(value, value => `Formatted: ${value}`);
Basic Example
import { useState, useDebugValue } from 'react';
function useOnlineStatus() {
const [isOnline, setIsOnline] = useState(true);
useDebugValue(isOnline); // Shows "true" or "false" in DevTools
return isOnline;
}
Example with Custom Formatting
function useFriendStatus(friendID) {
const [isOnline, setIsOnline] = useState(null);
useDebugValue(isOnline, online => (online ? ' Online' : ' Offline'));
return isOnline;
}
In React DevTools, this hook will now show a friendly label like:
useFriendStatus: Online
Notes
- Used only for debugging, it has no effect on app behavior.
- Appears only in development mode, not in production.
- Great for custom reusable hooks (not necessary in regular components).
Summary
Feature | Description |
---|---|
useDebugValue() | Adds debug label for custom hooks in DevTools |
Used inside | Custom hooks only |
Useful for | Showing state clearly in debugging tools |
Shown in | React Developer Tools (Components tab) |
Comments
Add new comment