Preact DevTools
Preact DevTools is a browser extension that provides a dedicated panel for inspecting, debugging, and profiling Preact applications. It allows you to examine component hierarchies, inspect props and state, track component updates, and measure performance.Installation
Browser Extensions
Install the Preact DevTools extension for your browser:- Chrome: Install from Chrome Web Store
- Firefox: Install from Firefox Add-ons
- Edge: Install from Edge Add-ons
Enabling DevTools Support
Preact automatically connects to DevTools when the extension is installed. To ensure compatibility, import the devtools connector:The
preact/debug package automatically imports preact/devtools, so if you’re using debug mode, you don’t need to import devtools separately.debug/src/index.js:2
How It Works
The devtools integration works by attaching to Preact’s options hooks:devtools/src/devtools.js:3-21
The extension injects a __PREACT_DEVTOOLS__ object into the global scope, which the devtools connector uses to establish communication between your app and the DevTools panel.
Features
Component Tree Inspection
The DevTools panel shows your component hierarchy in a tree structure:- View all rendered components
- See component relationships (parent/child)
- Identify component types (function vs class)
- Inspect Fragment boundaries
- Highlight components on hover
Props and State Inspection
Select any component to view its current state:- Props: Empty (no props passed)
- State:
{ 0: 0, 1: "Counter" }(hooks are numbered) - Hooks: List of all hooks used
Live Editing
Modify component state directly in DevTools:- Select a component
- Click on a state value
- Edit the value
- Press Enter to apply
Component Highlighting
When you hover over a component in DevTools, it gets highlighted in the page with a colored overlay showing:- Component boundaries
- Component name
- Dimensions
Source Code Location
DevTools can show where components are defined in your source code. This requires the Babel JSX source plugin:Profiling
The Profiler tab helps identify performance bottlenecks:- Click “Start Profiling”
- Interact with your app
- Click “Stop Profiling”
- Analyze the results
- Component render times
- Number of renders
- Why components re-rendered
- Flame graphs of render cycles
Custom Hook Names
By default, hooks appear as numbered entries in DevTools. You can provide custom labels usingaddHookName:
devtools/src/index.js:10-15
Now in DevTools, instead of seeing State: 0, you’ll see CustomValue: 0.
useDebugValue Alternative
Preact also supports React’suseDebugValue hook:
Debugging Class Components
For class components, DevTools shows:- Props: Component props
- State:
{ count: 0 } - Context: Any context values consumed
- Rendered by: Parent component
Debugging Context
When components use Context, DevTools shows the consumed values:ThemedButton will show:
- Hooks:
Context: "dark"
Performance Profiling Tips
Identifying Expensive Renders
- Open the Profiler tab
- Start recording
- Perform actions in your app
- Stop recording
- Look for components with long render times (shown in red/yellow)
Understanding Why Components Rendered
The profiler shows why each component re-rendered:- Props changed: Component received new props
- State changed: Component state was updated
- Parent rendered: Parent component re-rendered
- Context changed: Consumed context value changed
- Hooks changed: Hook dependencies changed
Flame Graph Analysis
The flame graph visualizes render hierarchy:- Width represents render duration
- Depth shows component nesting
- Color indicates render time (green = fast, red = slow)
Browser Console Integration
With DevTools installed, Preact exposes additional debugging helpers:$r - Selected Component
The currently selected component in DevTools is available as$r in the console:
Component Instance Access
For class components,$r is the component instance, so you can call methods:
Troubleshooting
DevTools Not Appearing
If the Preact panel doesn’t appear:- Verify installation: Check that the extension is installed and enabled
- Check imports: Ensure you import
preact/devtoolsorpreact/debug - Restart browser: Sometimes extensions need a restart
- Check console: Look for errors in the browser console
- Verify Preact: Make sure you’re actually using Preact, not React
DevTools Shows Empty Tree
If no components appear:- Ensure your app has rendered components
- Check that render was called:
render(<App />, container) - Verify devtools is imported before rendering
- Check for JavaScript errors preventing render
Performance Issues
DevTools adds overhead. If your app is slow with DevTools:- Close the DevTools panel when not needed
- Disable profiling when not actively profiling
- Use production builds for performance testing
- Close other browser extensions
Production Builds
Conditional Import
Build Configuration
Configure your bundler to exclude devtools in production:Standalone DevTools
For environments without browser extensions (like React Native or Electron), you can use the standalone DevTools:Comparison with React DevTools
Preact DevTools is similar to React DevTools but:- Lighter weight: Smaller extension footprint
- Preact-specific: Optimized for Preact’s architecture
- Hook numbering: Hooks are numbered by default (use
addHookNamefor labels) - Compatible: Can debug most Preact apps including those using preact/compat
Advanced: DevTools API
The DevTools connector exposes an API for advanced integrations:devtools/src/devtools.js:11-20
The DevTools API is internal and may change between versions. Use at your own risk.