Skip to content

Commit 688b780

Browse files
committed
Update README.md
1 parent 5bec3d8 commit 688b780

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,61 @@ The easiest way to apply several store enhancers in a row is to use the [`compos
8686

8787
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.
8888

89+
##### `store/configureStore.js`
90+
91+
```js
92+
import { createStore, applyMiddleware, compose } from 'redux';
93+
import rootReducer from '../reducers';
94+
import DevTools from '../containers/DevTools';
95+
96+
const finalCreateStore = compose(
97+
// Middleware you want to use in development:
98+
applyMiddleware(d1, d2, d3),
99+
// Required! Enable Redux DevTools with the monitors you chose
100+
DevTools.instrument()
101+
)(createStore);
102+
103+
export default function configureStore(initialState) {
104+
const store = finalCreateStore(rootReducer, initialState);
105+
106+
// Hot reload reducers (requires Webpack or Browserify HMR to be enabled)
107+
if (module.hot) {
108+
module.hot.accept('../reducers', () =>
109+
store.replaceReducer(require('../reducers')/*.default if you use Babel 6+ */)
110+
);
111+
}
112+
113+
return store;
114+
}
115+
```
116+
89117
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!
90118

119+
```js
120+
// ...
121+
import { persistState } from 'redux-devtools';
122+
123+
const finalCreateStore = compose(
124+
// Middleware you want to use in development:
125+
applyMiddleware(d1, d2, d3),
126+
// Required! Enable Redux DevTools with the monitors you chose
127+
DevTools.instrument(),
128+
// Optional. Lets you write ?debug_session=<key> in address bar to persist debug sessions
129+
persistState(getDebugSessionKey())
130+
)(createStore);
131+
132+
function getDebugSessionKey() {
133+
// You can write custom logic here!
134+
// By default we try to read the key from ?debug_session=<key> in the address bar
135+
const matches = window.location.href.match(/[?&]debug_session=([^&]+)\b/);
136+
return (matches && matches.length > 0)? matches[1] : null;
137+
}
138+
139+
export default function configureStore(initialState) {
140+
// ...
141+
}
142+
```
143+
91144
#### Exclude DevTools from Production Builds
92145

93146
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.

0 commit comments

Comments
 (0)