You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -19,89 +19,164 @@ A live-editing time travel environment for [Redux](https://github.com/rackt/redu
19
19
* If the reducers throw, you will see during which action this happened, and what the error was
20
20
* With `persistState()` store enhancer, you can persist debug sessions across page reloads
21
21
22
+
### Overview
23
+
24
+
Redux DevTools is a development time package that provides power-ups for your Redux development workflow. Be careful to strip its code in production! To use Redux DevTools, you need to choose a “monitor”—a React component that will serve as a UI for the DevTools. Different tasks and workflows require different UIs, so Redux DevTools is built to be flexible in this regard. We recommend using [`LogMonitor`](https://github.com/gaearon/redux-devtools-log-monitor) for inspecting the state and time travel, and wrap it in a [`DockMonitor`](https://github.com/gaearon/redux-devtools-dock-monitor) to quckly move it across the screen. That said, when you’re comfortable rolling up your own setup, feel free to do this, and share it with us.
25
+
22
26
### Installation
23
27
24
28
```
25
29
npm install --save-dev redux-devtools
26
30
```
27
31
28
-
DevTools is a [store enhancer](http://rackt.github.io/redux/docs/Glossary.html#store-enhancer), which should be added to your middleware stack *after*[`applyMiddleware`](http://rackt.github.io/redux/docs/api/applyMiddleware.html) as `applyMiddleware` is potentially asynchronous. Otherwise, DevTools won’t see the raw actions emitted by asynchronous middleware such as [redux-promise](https://github.com/acdlite/redux-promise) or [redux-thunk](https://github.com/gaearon/redux-thunk).
To use, first create a `DevTools` component by passing a `monitor`component to `createDevTools`. In the following example our `monitor` consists of [`redux-devtools-log-monitor`](https://github.com/gaearon/redux-devtools-log-monitor) docked within [`redux-devtools-dock-monitor`](https://github.com/gaearon/redux-devtools-dock-monitor):
43
+
Somewhere in your project, create a `DevTools` component by passing a `monitor`element to `createDevTools`. In the following example our `monitor` consists of [`LogMonitor`](https://github.com/gaearon/redux-devtools-log-monitor) docked within [`DockMonitor`](https://github.com/gaearon/redux-devtools-dock-monitor):
31
44
32
-
####containers/DevTools.js
45
+
##### `containers/DevTools.js`
33
46
34
47
```js
35
48
importReactfrom'react';
36
49
37
-
//createDevTools takes a monitor and produces a DevTools component
50
+
//Exported from redux-devtools
38
51
import { createDevTools } from'redux-devtools';
39
52
40
-
//Monitor component for Redux DevTools
53
+
//Monitors are separate packages, and you can make a custom one
41
54
importLogMonitorfrom'redux-devtools-log-monitor';
42
-
43
-
// Dock component to contain a Redux DevTools monitor
// createDevTools takes a monitor and produces a DevTools component
58
+
constDevTools=createDevTools(
59
+
// Monitors are individually adjustable with props
60
+
// Consult their repositories to learn about those props
61
+
<DockMonitor toggleVisibilityKey='ctrl-h'
62
+
changePositionKey='ctrl-q'>
63
+
<LogMonitor theme='tomorrow'/>
50
64
</DockMonitor>
51
65
);
66
+
67
+
exportdefaultDevTools;
68
+
```
69
+
70
+
Note that you can use `LogMonitor` directly without wrapping it in `DockMonitor` if you’d like to display the DevTools UI somewhere right inside your application:
71
+
72
+
```js
73
+
// If you'd rather not use docking UI, use <LogMonitor> directly
74
+
constDevTools=createDevTools(
75
+
<LogMonitor theme='solarized'/>
76
+
);
52
77
```
53
78
54
-
Note that it is not essential to put [`redux-devtools-log-monitor`](https://github.com/gaearon/redux-devtools-log-monitor) inside the dock component, it can be placed wherever you like in the component tree.
79
+
#### Use `DevTools.instrument()` Store Enhancer
80
+
81
+
The `DevTools` component you created with `createDevTools()` has a special static method called `instrument()`. It returns a [store enhancer](http://rackt.github.io/redux/docs/Glossary.html#store-enhancer) that you need to use in development.
82
+
83
+
A store enhancer is a function that takes `createStore` and returns an enhanced version of it that you will use instead. You probably already used another store enhancer—[`applyMiddleware()`](http://redux.js.org/docs/api/applyMiddleware.html). Unlike `applyMiddleware()`, you will need to be careful to only use `DevTools.instrument()` in development environment, and never in production.
84
+
85
+
The easiest way to apply several store enhancers in a row is to use the [`compose()`](http://redux.js.org/docs/api/compose.html) utility function that ships with Redux. It is the same `compose()` that you can find in Underscore and Lodash. In our case, we would use it to compose several store enhancers into one: `compose(applyMiddleware(m1, m2, m3), DevTools.instrument())`.
86
+
87
+
It’s important that you should add `DevTools.instrument()`*after*`applyMiddleware` in your `compose()` function arguments. This is because `applyMiddleware` is potentially asynchronous, but `DevTools.instrument()` expects all actions to be plain objects rather than actions interpreted by asynchronous middleware such as [redux-promise](https://github.com/acdlite/redux-promise) or [redux-thunk](https://github.com/gaearon/redux-thunk). So make sure `applyMiddleware()` goes first in the `compose()` call, and `DevTools.instrument()` goes after it.
55
88
56
-
Next add `instrument()` and (optionally) `persistState()` to your store enhancers, and create your store:
89
+
If you’d like, you may add another store enhancer called `persistState()`. It ships with this package, and it lets you serialize whole sessions (including all dispatched actions and the state of the monitors) by a URL key. So if you visit `http://localhost:3000/?debug_session=reproducing_weird_bug`, do something in the app, then open `http://localhost:3000/?debug_session=some_other_feature`, and then go back to `http://localhost:3000/?debug_session=reproducing_weird_bug`, the state will be restored. The implementation of `persistState()` is fairly naïve but you can take it as an inspiration and build a proper UI for it if you feel like it!
90
+
91
+
#### Exclude DevTools from Production Builds
92
+
93
+
Finally, to make sure we’re not pulling any DevTools-related code in the production builds, we will envify our code. With Webpack, you can use `DefinePlugin` (Browserify equivalent is called [`envify`](https://github.com/zertosh/loose-envify)) to turn magic constants like `process.env.NODE_ENV` into `'production'` or `'development'` strings depending on the environment, and import and render `redux-devtools` conditionally when `process.env.NODE_ENV` is not `'production'`. Then, if you have an Uglify step before production, Uglify will eliminate dead `if (false)` branches with `redux-devtools` imports.
94
+
95
+
If you are using ES6 modules with Webpack 1.x and Babel, you might try putting your `import` statement inside an `if (process.env.NODE_ENV !== 'production)` to exclude the DevTools package from your production bundle. However this ES6 specification forbids it, so this won’t compile. Instead, you can use a conditional CommonJS `require`. Babel will let it compile, and Uglify will eliminate the dead branches before Webpack creates a bundle. This is why we recommend creating a `configureStore.js` file that either directs you to `configureStore.dev.js` or `configureStore.prod.js` depending on the configuration. While it is a little bit more maintenance, the upside is that you can be sure you won’t pull any development dependencies into the production builds, and that you can easily enable different middleware (e.g. crash reporting, logging) in the production environment.
96
+
97
+
##### `store/configureStore.js`
98
+
99
+
```js
100
+
// Use ProvidePlugin (Webpack) or loose-envify (Browserify)
101
+
// together with Uglify to strip the dev branch in prod build.
//Hot reload reducers (requires Webpack or Browserify HMR to be enabled)
84
154
if (module.hot) {
85
155
module.hot.accept('../reducers', () =>
86
-
store.replaceReducer(require('../reducers'))
156
+
store.replaceReducer(require('../reducers')/*.default if you use Babel 6+ */)
87
157
);
88
158
}
89
159
90
160
return store;
91
161
}
92
162
```
93
163
94
-
Finally, include the DevTools component in your page:
164
+
#### Render `<DevTools>` in Your App
165
+
166
+
Finally, include the `DevTools` component in your page.
167
+
A naïve way to do this would be to render it right in your `index.js`:
95
168
96
-
####index.js
169
+
##### `index.js`
97
170
98
171
```js
99
172
importReactfrom'react';
100
173
import { render } from'react-dom';
101
174
import { Provider } from'react-redux';
102
175
importconfigureStorefrom'./store/configureStore';
103
-
importTodoAppfrom'./TodoApp';
104
-
importDevToolsfrom'./DevTools';
176
+
importTodoAppfrom'./containers/TodoApp';
177
+
178
+
// Don't do this! You’re bringing DevTools into the production bundle.
179
+
importDevToolsfrom'./containers/DevTools';
105
180
106
181
conststore=configureStore();
107
182
@@ -116,13 +191,9 @@ render(
116
191
);
117
192
```
118
193
119
-
**Make sure to only use DevTools in development!** In production it will be terribly slow because currently actions just accumulate forever.
120
-
121
-
In Webpack, you can use `DefinePlugin` to turn magic constants like `__DEV__` into `true` or `false` depending on the environment, and import and render `redux-devtools` conditionally behind `if (__DEV__)`. Then, if you have an Uglify step before production, Uglify will eliminate dead `if (false)` branches with `redux-devtools` imports.
122
-
123
-
If you are using ES6 modules with Webpack 1.x, you might try putting your `import` statement inside an `if (__DEV__)` to exclude the DevTools package from your production bundle. This will not work. However, you can work around this by creating separate `dev` and `prod` Root components that are dynamically imported using commonJS `require`:
194
+
We recommend a different approach. Create a `Root.js` component that renders the root of your application (usually some component surrounded by a `<Provider>`). Then use the same trick with conditional `require` statements to have two versions of it, one for development, and one for production:
124
195
125
-
####containers/Root.js
196
+
##### `containers/Root.js`
126
197
127
198
```js
128
199
if (process.env.NODE_ENV==='production') {
@@ -132,7 +203,7 @@ if (process.env.NODE_ENV === 'production') {
When you use [`DockMonitor`](https://github.com/gaearon/redux-devtools-dock-monitor), you usually want to render `<DevTools>` at the root of your app. It will appear in a docked container above it. However, you can also render it anywhere else in your React component tree. In this case, you’d create a development and a production version of some other component that would either include or exclude `<DevTools>`.
251
+
252
+
For example (you don’t have to do that!), you may prefer to display the DevTools in a separate window instead of rendering it inside the page. In this case, you can remove `DockMonitor` from `DevTools.js` and just use the `LogMonitor`, and have some code like this:
Personal preferences vary, and whether to put the DevTools in a separate window, in a dock, or right inside you app’s user interface, is up to you. Make sure to check the documentation for the monitors you use and learn about the different props they support for customizing the appearance and the behavior.
301
+
302
+
Note that there are no useful props you can pass to the `DevTools` component other than the `store`. The `store` prop is needed if you don’t wrap `<DevTools>` in a `<Provider>`—just like with any connected component. To adjust the monitors, you need to pass props to them inside `DevTools.js` itself inside the `createDevTools()` call when they are used.
Try clicking on actions in the log, or changing some code inside `examples/counter/reducers/counter`.
193
320
For fun, you can also open `http://localhost:3000/?debug_session=123`, click around, and then refresh.
194
321
195
-
Oh, and you can do the same with the TodoMVC example as well.
196
-
197
322
### Custom Monitors
198
323
199
324
**DevTools accepts monitor components so you can build a completely custom UI.**[`redux-devtools-log-monitor`](https://github.com/gaearon/redux-devtools-log-monitor) and [`redux-devtools-dock-monitor`](https://github.com/gaearon/redux-devtools-dock-monitor) are just examples of what is possible.
@@ -205,7 +330,7 @@ Some crazy ideas for custom monitors:
205
330
* A slider that lets you jump between computed states just by dragging it
206
331
* An in-app layer that shows the last N states right in the app (e.g. for animation)
207
332
* A time machine like interface where the last N states of your app reside on different Z layers
208
-
* Feel free to come up with and implement your own! Check [`redux-devtools-log-monitor`](https://github.com/gaearon/redux-devtools-log-monitor) propTypes to see what you can do.
333
+
* Feel free to come up with and implement your own! Check [`LogMonitor`](https://github.com/gaearon/redux-devtools-log-monitor) propTypes to see what you can do.
0 commit comments