React Developer Tools is a browser extension that allows debugging React applications:
// Component for debugging
function UserProfile({ userId }) {
const [user, setUser] = useState(null);
const [loading, setLoading] = useState(true);
// Visible in React DevTools
useEffect(() => {
fetchUser(userId).then(setUser);
}, [userId]);
return <div>{user?.name}</div>;
}React Developer Tools are like an X-ray for your React application! They allow you to look inside components and understand what’s happening under the hood. 🔍
# Chrome Web Store
# Firefox Add-ons
# Edge Add-onsnpm install -g react-devtools
react-devtoolsnpm install --save-dev react-devtools
npx react-devtoolsShows React component structure:
function App() {
return (
<div>
<Header />
<UserList users={users} />
<Footer />
</div>
);
}
function UserList({ users }) {
return (
<ul>
{users.map(user => (
<UserItem key={user.id} user={user} />
))}
</ul>
);
}Features:
Measures component rendering time:
function ExpensiveComponent({ data }) {
// Slow calculations
const processedData = useMemo(() => {
return data.map(item => heavyCalculation(item));
}, [data]);
return <div>{processedData.length} items</div>;
}Metrics:
View and edit component data:
function Counter() {
const [count, setCount] = useState(0);
const [name, setName] = useState('React');
return (
<div>
<h1>{name}: {count}</h1>
<button onClick={() => setCount(count + 1)}>
Increment
</button>
</div>
);
}Functions:
Monitor all hooks in component:
function UserProfile({ userId }) {
const [user, setUser] = useState(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);
const memoizedValue = useMemo(() => {
return user ? `${user.name} (${user.email})` : '';
}, [user]);
const debouncedSearch = useCallback(
debounce((query) => search(query), 300),
[]
);
return <div>{memoizedValue}</div>;
}Display:
View context providers:
const ThemeContext = createContext();
const UserContext = createContext();
function App() {
const [theme, setTheme] = useState('light');
const [user, setUser] = useState(null);
return (
<ThemeContext.Provider value={{ theme, setTheme }}>
<UserContext.Provider value={{ user, setUser }}>
<MainContent />
</UserContext.Provider>
</ThemeContext.Provider>
);
}Information:
Track asynchronous loading:
function App() {
return (
<ErrorBoundary>
<Suspense fallback={<Loading />}>
<LazyComponent />
</Suspense>
</ErrorBoundary>
);
}
const LazyComponent = lazy(() => import('./LazyComponent'));Monitoring:
// Problematic component
function SlowList({ items, filter }) {
// Without memoization - recalculation every render
const filteredItems = items.filter(item =>
item.name.includes(filter)
);
return (
<ul>
{filteredItems.map(item => (
<li key={item.id}>{item.name}</li>
))}
</ul>
);
}
// Optimized version
function FastList({ items, filter }) {
const filteredItems = useMemo(() =>
items.filter(item => item.name.includes(filter)),
[items, filter]
);
return (
<ul>
{filteredItems.map(item => (
<li key={item.id}>{item.name}</li>
))}
</ul>
);
}function BuggyComponent() {
const [data, setData] = useState([]);
const [loading, setLoading] = useState(false);
useEffect(() => {
setLoading(true);
fetchData()
.then(setData)
.finally(() => setLoading(false));
}, []); // Check dependencies in DevTools
return loading ? <Spinner /> : <DataList data={data} />;
}// Enable update highlighting
// Settings → General → Highlight updates when components render// Record profiling session
// Profiler → Start profiling → Interact with app → Stop profiling// Search in component tree
// Components → Search field → Type component name// Hide library components
// Settings → Components → Hide components where...function useCustomHook(value) {
const [state, setState] = useState(value);
// Will display in DevTools as "CustomHook"
useDebugValue(state > 10 ? 'High' : 'Low');
return [state, setState];
}function App() {
return (
<React.StrictMode>
<MyApp />
</React.StrictMode>
);
}import { Profiler } from 'react';
function onRenderCallback(id, phase, actualDuration) {
console.log('Component:', id, 'Phase:', phase, 'Duration:', actualDuration);
}
function App() {
return (
<Profiler id="App" onRender={onRenderCallback}>
<MyComponent />
</Profiler>
);
}❌ Wrong:
// Anonymous components are hard to debug
export default () => <div>Component</div>;✅ Correct:
// Named components are easier to find
function MyComponent() {
return <div>Component</div>;
}
MyComponent.displayName = 'MyComponent';
export default MyComponent;React Developer Tools are essential for development:
Use them to create fast and high-quality React applications! 🚀