Skip to content

Commit 2e1fd0a

Browse files
author
Scott Prue
committed
fix(docs): cleanup redux persist v5 example by including usage of autoRehydrate - prescottprue#512
1 parent fea298c commit 2e1fd0a

File tree

1 file changed

+67
-19
lines changed

1 file changed

+67
-19
lines changed

docs/integrations/redux-persist.md

Lines changed: 67 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,51 +7,99 @@ Usage with [redux-persist](https://github.com/rt2zz/redux-persist) depends on wh
77
*createStore.js*
88

99
```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'
1313
import firebase from 'firebase/app'
1414
import 'firebase/auth'
1515
import 'firebase/database'
1616
import { persistStore, persistReducer, autoRehydrate } from 'redux-persist'
1717
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'
1920
import {
2021
firebase as firebaseConfig, // firebase config
2122
reduxFirebase as reduxConfig // redux-persist config
2223
} from '../config'
2324

2425
const persistConfig = {
2526
key: 'root',
26-
storage,
27+
storage: localStorage
2728
}
2829

2930
export default (initialState = {}, history) => {
3031
// Initialize firebase instance
3132
firebase.initializeApp(firebaseConfig)
3233

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())
3735

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))
4647

47-
const persistedReducer = persistReducer(persistConfig, rootReducer)
48-
const store = createStoreWithFirebase(persistedReducer, initialState)
4948
const persistor = persistStore(store)
5049

5150
return { store, persistor }
5251
}
5352
```
5453

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+
55103
*App.js*
56104

57105
```js

0 commit comments

Comments
 (0)