Building Your Learning Module...
Getting things ready for you!
Find videos you like?
Save to resource drawer for future reference!
useDebugValue displays a custom label for custom Hooks in React DevTools. It helps you understand what's happening inside your custom Hooks during development by showing meaningful debug information next to your Hook in the Components tab.
Hooks that are part of a shared library used by multiple teams
Hooks with complex internal state that's hard to inspect
When raw value needs formatting to be human-readable
Don't add useDebugValue to every custom Hook! Only use it when the debug info significantly improves the debugging experience.
Start by creating a custom hook that manages some state or logic. This will be the foundation for adding debug values.
Pro Tip
Custom hooks should start with "use" and can manage any combination of state, effects, and logic.
Add useDebugValue to display meaningful information in React DevTools. Choose a value that helps you understand the hook's state.
Pro Tip
Use simple, human-readable values. The debug value appears next to your hook name in DevTools!
For complex values, use a formatter function to create readable labels. The formatter runs only when DevTools is open, optimizing performance.
Pro Tip
Formatters are lazy! They only run when DevTools is open, making them perfect for expensive operations.
Open React DevTools to see your debug values in action. The debug label appears next to your custom hook in the Components tab.
DevTools View
In DevTools: useOnlineStatus: "Online" or useOnlineStatus: "Offline"
Follow these guidelines to make the most of useDebugValue in your custom hooks.
Golden Rule
Use useDebugValue when the debug information significantly improves the debugging experience for you or your team.
Shows connection status in DevTools
function useOnlineStatus() {
const [isOnline, setIsOnline] = React.useState(true);
React.useEffect(() => {
function handleOnline() {
setIsOnline(true);
}
function handleOffline() {
setIsOnline(false);
}
window.addEventListener('online', handleOnline);
window.addEventListener('offline', handleOffline);
return () => {
window.removeEventListener('online', handleOnline);
window.removeEventListener('offline', handleOffline);
};
}, []);
// Show debug label in DevTools
React.useDebugValue(isOnline ? 'Online' : 'Offline');
return isOnline;
}
function StatusIndicator() {
const isOnline = useOnlineStatus();
return (
<div className="container">
<h1>🌐 Connection Status</h1>
<div className={`status ${isOnline ? 'online' : 'offline'}`}>
<div className="icon">
{isOnline ? '✅' : '❌'}
</div>
<div className="text">
<h2>{isOnline ? 'Online' : 'Offline'}</h2>
<p>
{isOnline
? 'Connected to the internet'
: 'No internet connection'}
</p>
</div>
</div>
<div className="info">
💡 Check React DevTools to see the debug value!
</div>
<div className="note">
<strong>Open DevTools:</strong> The custom hook will show
"Online" or "Offline" label in the Components tab
</div>
</div>
);
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<StatusIndicator />);Loading preview...
Only visible in React DevTools during development. Helps identify hook state at a glance.
Formatter function runs only when DevTools is open, optimizing performance in production.
Completely removed from production builds. No runtime overhead or bundle size impact.