Last active
August 6, 2019 10:04
-
-
Save behnammodi/e25fb27b2f8288d9e0c416ffc437ddc7 to your computer and use it in GitHub Desktop.
Redux structure
This file contains 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
export default { | |
ACTIONS_INC: state => ({ | |
...state, | |
count: state.count + 1 | |
}), | |
ACTIONS_DEC: state => ({ ...state, count: state.count - 1 }) | |
}; |
This file contains 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, 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; |
This file contains 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
export default JSON.parse(localStorage.getItem("store")) || { | |
count: 0, | |
num: 0 | |
}; |
This file contains 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
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); | |
} | |
}; |
This file contains 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 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