Mastering useDebugValue
in React: Debug Smarter, Code Better
In modern React development, building robust components goes beyond functional rendering. Understanding how to introspect internal behavior in development mode can lead to better debugging, faster optimization, and clearer collaboration. One powerful yet underused tool in this realm is useDebugValue
.
In this article, we’ll explore how to harness useDebugValue
like a React expert, including:
- What
useDebugValue
is and when to use it - How it integrates with custom hooks
- Real-world use cases through a math-themed component
- Bonus tips for improving developer experience
What is useDebugValue
?
useDebugValue
is a React Hook used to display a label for custom hooks in React Developer Tools. It does not affect behavior in production and is completely safe to use for debugging purposes only.
useDebugValue(value);
useDebugValue(value, formatFn);
-
value
: any data you want to display in React DevTools -
formatFn
: an optional function that customizes the label shown
✅ It only appears in React DevTools, helping you understand what’s happening inside custom hooks.
Why Use useDebugValue
?
Imagine debugging a component that uses several hooks under the hood—useAuth
, useForm
, useWaveTimer
. When you open DevTools, instead of seeing unclear internal state, useDebugValue
shows what each custom hook holds at a glance.
Without useDebugValue
:
CustomHook
value: Object
With useDebugValue
:
useWaveTimer: angle=45, value=0.707
This insight saves you from repeatedly adding console.log
everywhere.
Real Example: Math with useDebugValue
Let’s build a custom useSineTimer
hook that uses useState
, useRef
, useEffect
, and useDebugValue
to simulate a real-time sine wave update.
Custom Hook: useSineTimer.ts
import { useState, useEffect, useRef, useDebugValue } from 'react';
export const useSineTimer = () => {
const [angle, setAngle] = useState(0);
const [value, setValue] = useState(0);
const timerRef = useRef<NodeJS.Timeout | null>(null);
useEffect(() => {
timerRef.current = setInterval(() => {
setAngle(prev => {
const nextAngle = prev + 15;
setValue(Math.sin(nextAngle * Math.PI / 180));
return nextAngle;
});
}, 1000);
return () => {
if (timerRef.current) {
clearInterval(timerRef.current);
}
};
}, []);
useDebugValue(`angle=${angle}, value=${value.toFixed(4)}`);
return { angle, value };
};
Component: SineWaveTimer.tsx
import React from 'react';
import { useSineTimer } from './useSineTimer';
export const SineWaveTimer = () => {
const { angle, value } = useSineTimer();
return (
<div className="math-card">
<h3>📈 Sine Timer Simulation</h3>
<p>Angle: {angle} degrees</p>
<p>Sine Value: {value.toFixed(4)}</p>
</div>
);
};
Best Practices
Tip | Description |
---|---|
Only use in dev |
useDebugValue runs only in development, never affects production. |
Combine with formatFn | If the value is complex, add a formatter. |
Use in custom hooks | Do not use useDebugValue in regular components. |
Group debug info | Return formatted strings or structured summaries. |
Example with formatFn
:
useDebugValue({ angle, value }, v => `angle=${v.angle}, sin=${v.value.toFixed(2)}`);
When Not to Use useDebugValue
- Inside regular components – It’s designed for custom hooks only.
- For production diagnostics – It’s removed in builds.
- For large logs – Use minimal display info, avoid dumping big objects.
Bonus Challenge
Want to master hooks even more?
- Create a
useCircleAreaTracker(radius)
hook. - Show computed area with
useDebugValue
. - Build a visual dashboard tracking multiple shapes.
Conclusion
React’s useDebugValue
is a precision tool for mastering debugging in custom hooks. It enhances observability in DevTools, leads to cleaner console-free insights, and boosts collaboration for teams building reusable logic.
By learning how to use it in practical scenarios—like our useSineTimer
—you’ll write smarter, cleaner, and more maintainable React code.
Now you have the formula. Use it well. 🧪⚛️
#react #hooks #typescript #frontend #debugging