Skip to content

Commit 7f5d4bd

Browse files
committed
Add first-class support for store enhancers to createStore() API
1 parent ce533b4 commit 7f5d4bd

File tree

6 files changed

+35
-24
lines changed

6 files changed

+35
-24
lines changed

examples/async/store/configureStore.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@ import thunkMiddleware from 'redux-thunk'
33
import createLogger from 'redux-logger'
44
import rootReducer from '../reducers'
55

6-
const createStoreWithMiddleware = applyMiddleware(
7-
thunkMiddleware,
8-
createLogger()
9-
)(createStore)
10-
116
export default function configureStore(initialState) {
12-
const store = createStoreWithMiddleware(rootReducer, initialState)
7+
const store = createStore(
8+
rootReducer,
9+
initialState,
10+
applyMiddleware(thunkMiddleware, createLogger()),
11+
)
1312

1413
if (module.hot) {
1514
// Enable Webpack hot module replacement for reducers

examples/real-world/store/configureStore.dev.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ import createLogger from 'redux-logger'
88
import rootReducer from '../reducers'
99

1010
const reduxRouterMiddleware = syncHistory(browserHistory)
11-
const finalCreateStore = compose(
12-
applyMiddleware(thunk, api, reduxRouterMiddleware, createLogger()),
13-
DevTools.instrument()
14-
)(createStore)
1511

1612
export default function configureStore(initialState) {
17-
const store = finalCreateStore(rootReducer, initialState)
13+
const store = createStore(
14+
rootReducer,
15+
initialState,
16+
applyMiddleware(thunk, api, reduxRouterMiddleware, createLogger()),
17+
DevTools.instrument()
18+
)
1819

1920
// Required for replaying actions from devtools to work
2021
reduxRouterMiddleware.listenForReplays(store)

examples/real-world/store/configureStore.prod.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import thunk from 'redux-thunk'
55
import api from '../middleware/api'
66
import rootReducer from '../reducers'
77

8-
const finalCreateStore = compose(
9-
applyMiddleware(thunk, api, syncHistory(browserHistory)),
10-
)(createStore)
11-
128
export default function configureStore(initialState) {
13-
return finalCreateStore(rootReducer, initialState)
9+
return createStore(
10+
rootReducer,
11+
initialState,
12+
applyMiddleware(thunk, api, syncHistory(browserHistory))
13+
)
1414
}

examples/shopping-cart/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ const middleware = process.env.NODE_ENV === 'production' ?
1212
[ thunk ] :
1313
[ thunk, logger() ]
1414

15-
const createStoreWithMiddleware = applyMiddleware(...middleware)(createStore)
16-
const store = createStoreWithMiddleware(reducer)
15+
const store = createStore(
16+
reducer,
17+
applyMiddleware(...middleware)
18+
)
1719

1820
store.dispatch(getAllProducts())
1921

examples/universal/common/store/configureStore.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ import { createStore, applyMiddleware } from 'redux'
22
import thunk from 'redux-thunk'
33
import rootReducer from '../reducers'
44

5-
const createStoreWithMiddleware = applyMiddleware(
6-
thunk
7-
)(createStore)
8-
95
export default function configureStore(initialState) {
10-
const store = createStoreWithMiddleware(rootReducer, initialState)
6+
const store = createStore(
7+
rootReducer,
8+
initialState,
9+
applyMiddleware(thunk)
10+
)
1111

1212
if (module.hot) {
1313
// Enable Webpack hot module replacement for reducers

src/createStore.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import isPlainObject from './utils/isPlainObject'
2+
import compose from './compose'
23

34
/**
45
* These are private action types reserved by Redux.
@@ -30,7 +31,15 @@ export var ActionTypes = {
3031
* @returns {Store} A Redux store that lets you read the state, dispatch actions
3132
* and subscribe to changes.
3233
*/
33-
export default function createStore(reducer, initialState) {
34+
export default function createStore(reducer, initialState, ...enhancers) {
35+
if (typeof initialState === 'function') {
36+
enhancers.unshift(initialState)
37+
initialState = undefined
38+
}
39+
if (enhancers.length > 0) {
40+
return compose(...enhancers)(createStore)(reducer, initialState)
41+
}
42+
3443
if (typeof reducer !== 'function') {
3544
throw new Error('Expected the reducer to be a function.')
3645
}

0 commit comments

Comments
 (0)