@@ -7,51 +7,99 @@ Usage with [redux-persist](https://github.com/rt2zz/redux-persist) depends on wh
7
7
* createStore.js*
8
8
9
9
``` js
10
- import { Provider } from ' react-redux '
11
- import { createStore , combineReducers , compose } from ' redux'
12
- import { reactReduxFirebase , firebaseReducer } from ' react-redux-firebase'
10
+ import { browserHistory } from ' react-router '
11
+ import { createStore , compose } from ' redux'
12
+ import { reactReduxFirebase } from ' react-redux-firebase'
13
13
import firebase from ' firebase/app'
14
14
import ' firebase/auth'
15
15
import ' firebase/database'
16
16
import { persistStore , persistReducer , autoRehydrate } from ' redux-persist'
17
17
import localStorage from ' redux-persist/lib/storage' // defaults to localStorage for web and AsyncStorage for react-native
18
- import hardSet from ' redux-persist/lib/stateReconciler/hardSet'
18
+ import makeRootReducer from ' ./reducers'
19
+ import { updateLocation } from ' ./location'
19
20
import {
20
21
firebase as firebaseConfig , // firebase config
21
22
reduxFirebase as reduxConfig // redux-persist config
22
23
} from ' ../config'
23
24
24
25
const persistConfig = {
25
26
key: ' root' ,
26
- storage,
27
+ storage: localStorage
27
28
}
28
29
29
30
export default (initialState = {}, history ) => {
30
31
// Initialize firebase instance
31
32
firebase .initializeApp (firebaseConfig)
32
33
33
- // Add reactReduxFirebase store enhancer when making store creator
34
- const createStoreWithFirebase = compose (
35
- reactReduxFirebase (firebase, rrfConfig), // firebase instance as first argument
36
- )(createStore)
34
+ const persistedReducer = persistReducer (persistConfig, makeRootReducer ())
37
35
38
- // Add firebase to reducers (uses persistReducer and hardSet)
39
- const rootReducer = combineReducers ({
40
- firebase: persistReducer (
41
- { key: ' firepersist' , storage: localStorage, stateReconciler: hardSet },
42
- firebaseReducer
43
- ),
44
- anotherReducer
45
- });
36
+ const store = createStore (
37
+ persistedReducer,
38
+ initialState,
39
+ compose (
40
+ reactReduxFirebase (firebase, reduxConfig),
41
+ autoRehydrate ()
42
+ )
43
+ )
44
+
45
+ // To unsubscribe, invoke `store.unsubscribeHistory()` anytime
46
+ store .unsubscribeHistory = browserHistory .listen (updateLocation (store))
46
47
47
- const persistedReducer = persistReducer (persistConfig, rootReducer)
48
- const store = createStoreWithFirebase (persistedReducer, initialState)
49
48
const persistor = persistStore (store)
50
49
51
50
return { store, persistor }
52
51
}
53
52
```
54
53
54
+ * location.js*
55
+
56
+ ``` js
57
+ // Constants
58
+ export const LOCATION_CHANGE = ' LOCATION_CHANGE'
59
+
60
+ // Actions
61
+ export function locationChange (location = ' /' ) {
62
+ return {
63
+ type: LOCATION_CHANGE ,
64
+ payload: location
65
+ }
66
+ }
67
+
68
+ // Specialized Action Creator
69
+ export function updateLocation ({ dispatch }) {
70
+ return nextLocation => dispatch (locationChange (nextLocation))
71
+ }
72
+
73
+ // Reducer
74
+ const initialState = null
75
+ export default function locationReducer (state = initialState , action ) {
76
+ return action .type === LOCATION_CHANGE ? action .payload : state
77
+ }
78
+ ```
79
+
80
+ * reducers.js*
81
+
82
+ ``` js
83
+ import { combineReducers } from ' redux'
84
+ import { firebaseReducer as firebase } from ' react-redux-firebase'
85
+ import { persistReducer } from ' redux-persist'
86
+ import localStorage from ' redux-persist/lib/storage' // defaults to localStorage for web and AsyncStorage for react-native
87
+ import hardSet from ' redux-persist/lib/stateReconciler/hardSet'
88
+ import locationReducer from ' ./location'
89
+
90
+ export default function makeRootReducer () {
91
+ return combineReducers ({
92
+ // Add sync reducers here
93
+ firebase: persistReducer (
94
+ { key: ' firebaseState' , storage: localStorage, stateReconciler: hardSet },
95
+ firebase
96
+ ),
97
+ location: locationReducer
98
+ })
99
+ }
100
+ ```
101
+
102
+
55
103
* App.js*
56
104
57
105
``` js
0 commit comments