Skip to content

Instantly share code, notes, and snippets.

@behnammodi
Last active August 6, 2019 10:04
Show Gist options
  • Save behnammodi/e25fb27b2f8288d9e0c416ffc437ddc7 to your computer and use it in GitHub Desktop.
Save behnammodi/e25fb27b2f8288d9e0c416ffc437ddc7 to your computer and use it in GitHub Desktop.
Redux structure
export default {
ACTIONS_INC: state => ({
...state,
count: state.count + 1
}),
ACTIONS_DEC: state => ({ ...state, count: state.count - 1 })
};
import { createStore, applyMiddleware } from "redux";
import reducer from "./reducer";
import middlewares from "./middlewares";
import initialState from "./initial-state";
const store = createStore(
reducer,
initialState,
applyMiddleware(middlewares.log, middlewares.storage)
);
export default store;
export default JSON.parse(localStorage.getItem("store")) || {
count: 0,
num: 0
};
export default {
log: store => next => action => {
if (action.type === "ACTIONS_INC") {
console.log("From middlewareLog", store.getState());
}
return next(action);
},
storage: store => next => action => {
setTimeout(() => {
localStorage.setItem("store", JSON.stringify(store.getState()));
});
return next(action);
}
};
import actions from "./actions";
export default (state, { type }) => {
let currentState = state;
switch (type) {
case "ACTIONS_INC":
currentState = actions.ACTIONS_INC(state);
break;
case "ACTIONS_DEC":
currentState = actions.ACTIONS_DEC(state);
break;
}
return currentState;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment