Last active
April 22, 2017 05:55
-
-
Save jeswin/ae3aaa9dd5a52b38ce0b1e93f52493f7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { createStore as reduxCreateStore } from "redux"; | |
const initialState = { | |
selectedReddit: "reactjs", | |
postsByReddit: {} | |
}; | |
let store; | |
export function createStore() { | |
const args = [].slice.call(arguments); | |
store = reduxCreateStore.apply( | |
undefined, | |
[ | |
reducer, | |
initialState, | |
window.__REDUX_DEVTOOLS_EXTENSION__ && | |
window.__REDUX_DEVTOOLS_EXTENSION__() | |
].concat(args) | |
); | |
return store; | |
} | |
function reducer(state, action) { | |
return action && action.__replaceState ? action.state : state; | |
} | |
export function getState(selector) { | |
return selector ? selector(store.getState()) : store.getState(); | |
} | |
export function updateState(property, update, actionType) { | |
const state = store.getState(); | |
const newState = { ...state, [property]: update(state[property]) }; | |
store.dispatch({ | |
type: actionType || "REPLACE_STATE", | |
__replaceState: true, | |
state: newState | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment