-
Notifications
You must be signed in to change notification settings - Fork 28.1k
/
Copy pathinitRedux.js
39 lines (33 loc) · 1.06 KB
/
initRedux.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import { createStore, combineReducers, applyMiddleware, compose } from 'redux'
import reducers from './reducers'
let reduxStore = null
// Get the Redux DevTools extension and fallback to a no-op function
let devtools = f => f
if (process.browser && window.__REDUX_DEVTOOLS_EXTENSION__) {
devtools = window.__REDUX_DEVTOOLS_EXTENSION__()
}
function create (apollo, initialState = {}) {
return createStore(
combineReducers({ // Setup reducers
...reducers,
apollo: apollo.reducer()
}),
initialState, // Hydrate the store with server-side data
compose(
applyMiddleware(apollo.middleware()), // Add additional middleware here
devtools
)
)
}
export default function initRedux (apollo, initialState) {
// Make sure to create a new store for every server-side request so that data
// isn't shared between connections (which would be bad)
if (!process.browser) {
return create(apollo, initialState)
}
// Reuse store on the client-side
if (!reduxStore) {
reduxStore = create(apollo, initialState)
}
return reduxStore
}