-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.jsx
executable file
·35 lines (29 loc) · 1.01 KB
/
store.jsx
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
import { createStore, combineReducers, applyMiddleware } from "redux";
import weatherReducer from "./weather-reducer";
import thunkMiddleware from 'redux-thunk'
const saveToLocalStorage = state => {
try {
let serializedState = JSON.stringify(state)
localStorage.setItem('state', serializedState)
} catch(e) {
console.log(e)
}
}
const loadFromLocalStorage = () => {
try{
let serializedState = localStorage.getItem('state')
if(serializedState === null) return undefined
return JSON.parse(serializedState)
}catch(e) {
console.log(e)
return undefined
}
}
const reducers = combineReducers({
weatherReducer
})
const persistedState = loadFromLocalStorage()
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__
const store = createStore(reducers,persistedState, composeEnhancers(applyMiddleware(thunkMiddleware)))
store.subscribe( () => saveToLocalStorage(store.getState()) )
export default store