Skip to content

Instantly share code, notes, and snippets.

@jeswin
Last active April 22, 2017 05:55
Show Gist options
  • Save jeswin/ae3aaa9dd5a52b38ce0b1e93f52493f7 to your computer and use it in GitHub Desktop.
Save jeswin/ae3aaa9dd5a52b38ce0b1e93f52493f7 to your computer and use it in GitHub Desktop.
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