Debug Redux Apps Easily with Redux DevTools
Introduction
Redux has become one of the most popular state management libraries for JavaScript applications. It provides a predictable state container and strict rules for mutating state, making it easier to understand data flow and debug applications. However, debugging Redux apps can still be challenging without the right tools.
When an application's state and UI becomes out of sync or an action does not trigger the expected result, debugging the issue without visibility into the Redux store and actions can be incredibly frustrating. You may find yourself inserting console.logs everywhere or attempting to reason through layers of complex logic in your head.
This is where Redux DevTools comes in. Redux DevTools is an invaluable browser extension that gives you complete visibility into your Redux apps. With Redux DevTools, you can inspect every state value, view the history of dispatched actions, dispatch actions manually, trace async flows, export state snapshots, and much more.
Redux DevTools provides a powerful suite of debugging capabilities that will accelerate your Redux development. From small hobby projects to large enterprise applications, integrating Redux DevTools should be a standard best practice for any Redux codebase.
What is Redux DevTools
Redux DevTools is a browser extension that allows you to debug Redux apps with ease. It provides complete visibility into the Redux store, actions, and state changes happening in your app.
With Redux DevTools, you can inspect the current state values, view the history of dispatched actions, dispatch new actions manually, trace async action flows, export state snapshots, and much more. It connects seamlessly with any Redux app and comes with built-in bindings for React-Redux, Redux Toolkit, and other libraries.
The DevTools extension was created and is maintained by the Redux core team. It's an open source tool available on GitHub. The extension is available for Chrome, Firefox, and other major browsers. It works with any Redux app as long as you have integrated the Redux bindings.
Redux DevTools gives you a central place to debug all things Redux. Instead of inserting console logs everywhere, you can leverage the powerful debugging utilities provided by Redux DevTools. It's an invaluable tool for inspecting state, tracking actions, and understanding complex Redux app behavior.
Key Features of Redux DevTools
Let's explore some of the key features provided by Redux DevTools that make it so useful for debugging Redux applications:
Time-Travel Debugging
One of the most powerful Redux DevTools features is time-travel debugging. It allows you to rewind the state history and "time-travel" back to previous points. You can then replay actions forward and view how the state evolves.
This enables you to pinpoint exactly when a bug occurred and understand the sequence of actions leading to it. Time-travel debugging is an invaluable tool for diagnosing issues in complex Redux apps.
State Inspector
The state inspector gives you a detailed view into the current values of your entire Redux state tree. You can drill down into nested state objects, search across keys and values, and see a history of state changes.
Having easy access to view state helps you quickly debug many common issues like identifying where bad state values are being introduced.
Action Logger
Redux DevTools logs every action dispatched, along with metadata like the action type, payload, timestamp, stack trace, and more. You can filter actions and scan through the history to understand the sequence of events.
The action logger is tremendously helpful for verifying if certain actions are being dispatched as expected. You can track down issues where actions are missing, duplicated, or out of order.
Dispatch Tool
Manually dispatching actions is a great way to test behavior and induce state changes. The dispatch tool allows you to trigger actions directly from the DevTools UI.
You can use this to populate test data, isolate parts of state logic, or reproduce bugs by forcing a specific sequence of actions that led to the issue.
Tracing Capabilities
Redux DevTools allows you to trace various events happening in your app including actions, state changes, and async calls. Tracing helps visualize the flow of events and pinpoint performance bottlenecks.
You can filter traces to focus on a particular subset of actions or state keys. The tracing view displays detailed timings and stack traces for each event.
Import/Export State Snapshots
With Redux DevTools, you can export a snapshot of the current Redux state. This allows you to easily share state for debugging issues or collaborate with other developers.
You can also import previously exported snapshots. This enables "time jumping" directly to important states rather than needing to replay all actions leading up to that point.
React Native Debugger Integration
For React Native apps using Redux, you can integrate React Native Debugger with Redux DevTools to enable remote debugging. This gives you access to all the same powerful debugging tools when running on mobile devices.
With Redux DevTools and React Native Debugger, you get full visibility into the Redux state and actions in your mobile apps. You can inspect state, track actions, export snapshots, and replay state history to debug issues. It provides a seamless debugging experience across web and mobile Redux applications.
Setting Up Redux DevTools
Let's go through the steps for setting up Redux DevTools in your apps:
Installation
First, you'll need to install the Redux DevTools browser extension. It's available for Chrome, Firefox, Edge, and other major browsers. Refer to the Redux DevTools installation docs for details on getting the latest version.
Make sure to install the appropriate version that is compatible with the Redux binding libraries in your project. For example, React-Redux v4 requires Redux DevTools v3, while React-Redux v5 onwards works with Redux DevTools v4.
Integrating Bindings
Next, you need to integrate the Redux bindings into your Redux app. This connects Redux DevTools to your store.
If you're using React-Redux, the composeWithDevTools
function handles wrapping the store with the bindings:
import { createStore } from 'redux'
import { composeWithDevTools } from 'redux-devtools-extension'
const store = createStore(rootReducer, composeWithDevTools())
For Redux Toolkit apps, configureStore
and createDevStore
have built-in support for Redux DevTools:
import { configureStore } from '@reduxjs/toolkit'
const store = configureStore({
// ...
})
You can use the store enhancer approach above, or use the Redux DevTools middleware for maximum flexibility.
Toggling Environments
In production, you'll want to ensure Redux DevTools is disabled to avoid any performance impact.
Conditionally apply the bindings based on the NODE_ENV
environment variable, or an isDevelopment
flag in your code.
let devTools = f => f
if (process.env.NODE_ENV === 'development') {
const { devToolsExtension } = require('redux-devtools-extension')
devTools = devToolsExtension()
}
const store = createStore(
rootReducer,
composeWithDevTools(devTools)
)
This prevents Redux DevTools from being enabled in production builds.
Using Redux DevTools
Let's walk through using the various Redux DevTools features to debug actual issues:
Basic Debugging Features
The Redux DevTools panel provides several core utilities that will cover many common debugging needs:
Inspecting State
View all top-level keys in the state tree and drill down into nested objects. This helps debug issues where some state value is incorrect.
Time-Travel Debugging
Rewind the state history and replay actions to pinpoint when a bug first occurred. You can diagnose the specific sequence of actions that led to the issue.
Dispatching Actions
Manually trigger actions from the "Dispatch" tab to change state or reproduce a bug. Useful for testing in isolation.
Importing/Exporting
Import previous state snapshots or save the current state. This enables sharing specific states across devices or with other developers.
Toggling Action Monitoring
Disable or enable action logging to filter noise and focus on relevant events. Turn off persistence to only track actions since loading the page.
Advanced Features
Redux DevTools provides powerful advanced capabilities:
State Charting
View history charts of state changes over time. Helps identify sudden changes or spot trends. Can also chart action duration to find performance issues.
Action Logging
Log all dispatched actions with metadata like type, payload, timestamp, stack traces, etc. Filter and search logs to isolate specific actions.
State/Action Tracing
Trace async flows and performance. Filter traces to focus on specific actions or state keys. View detailed timings and stack traces.
React Native Debugger
Integrate with React Native Debugger to enable remote Redux debugging on mobile devices. Same DevTools capabilities.
Custom Logging
Redux middleware and extensions add features like logging state snapshots, monitoring selector results, logging to external services, and more.
Tips and Best Practices
Here are some tips for getting the most from Redux DevTools:
- Start debugging early in development to establish visibility into typical app behavior.
- Learn keyboard shortcuts like toggling actions, jumping to state, dispatching. Drastically improves debugging speed.
- Don't rely solely on Redux DevTools. Use it combined with logging, breakpoints, React debugging tools, etc.
- Trace actions and state keys related to the specific issue you're debugging to avoid noise.
- Export/import state snapshots often to save important debugging states.
- Disable DevTools in production to eliminate performance impact. Only enable in development.
Conclusion
Redux DevTools provides an invaluable set of debugging utilities for inspecting and understanding Redux state, actions, and behavior. The detailed visibility helps diagnose issues quickly and allows you to develop Redux apps with confidence.
Features like time-travel debugging, tracing capabilities, and state importing/exporting accelerate debugging complex Redux apps. Redux DevTools is a free, open source tool that integrates seamlessly with any Redux app.
Platforms like DevHunt showcase essential developer tools like Redux DevTools to help build robust applications. With Redux DevTools, you can inspect state, track actions, and replay debugging sessions on both web and mobile apps. Whether you're just starting out with Redux or building large-scale apps, leverage the powerful debugging capabilities of Redux DevTools.