Advertisement
tringuyen25

Redux Example

Dec 21st, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. reducer.index.jsx
  2.  
  3. import match_options, * as matchOptionsSelectors from "./match_options.reducer.jsx";
  4. const galaxyApp = combineReducers({
  5.     match_options
  6. });
  7.  
  8. // GETTER
  9. export const getMatchOptions = (state) => {
  10.     return matchOptionsSelectors.getMatchOptions(state.match_options);
  11. };
  12.  
  13. export default galaxyApp;
  14.  
  15. ______________________________________________________________________________________
  16.  
  17. index.jsx
  18.  
  19. import { createStore, applyMiddleware } from "redux";
  20. import appReducer from "./reducers/reducer.index.jsx";
  21. const store = createStore(appReducer, applyMiddleware(thunk));
  22.  
  23. ______________________________________________________________________________________
  24.  
  25. ACTIONS.jsx
  26.  
  27. // SETTER
  28. const ACTION_TYPES = {
  29.     SET_MATCH_OPTIONS: "SET_MATCH_OPTIONS",
  30. }
  31.  
  32. export default ACTION_TYPES;
  33.  
  34. ______________________________________________________________________________________
  35.  
  36. action.index.jsx
  37.  
  38. import ACTION_TYPES from "../config/ACTIONS.jsx";
  39.  
  40. // SETTER
  41. export default {
  42.     setMatchOptions(header, text) {
  43.         return {
  44.             type: ACTION_TYPES.SET_MATCH_OPTIONS,
  45.             header: header,
  46.             text: text,
  47.         };
  48.     }
  49. };
  50.  
  51. ______________________________________________________________________________________
  52.  
  53. match_options.reducer.jsx
  54.  
  55. import ACTION_TYPES from "../config/ACTIONS.jsx";
  56. const INIT_STATE = {
  57.     match_options: {},
  58. };
  59.  
  60. // SETTER
  61. const match_options = (state = INIT_STATE, action) => {
  62.     let new_state;
  63.     switch (action.type) {
  64.         case ACTION_TYPES.SET_MATCH_OPTIONS:
  65.             new_state = Object.assign({}, INIT_STATE);
  66.             new_state.match_options[action.header] = action.text;
  67.             return new_state;
  68.         default:
  69.             return state;
  70.     }
  71. };
  72.  
  73. export default match_options;
  74.  
  75. // GETTER
  76. export const getMatchOptions = (state) => {
  77.     return state.match_options;
  78. };
  79.  
  80. ______________________________________________________________________________________
  81.  
  82. USAGE:
  83.  
  84. // GETTER
  85. const mapStateToProps = (state) => ({
  86.     match_options: reduxSelector.getMatchOptions(state),
  87. });
  88.  
  89. // SETTER
  90. const mapDispatchtoProps = dispatch => {
  91.     return {
  92.         setMatchOptions: (header, text) => {
  93.             dispatch(Action.setMatchOptions(header, text));
  94.         },
  95.     };
  96. };
  97.  
  98. export default connect(
  99.     mapStateToProps,
  100.     mapDispatchtoProps
  101. )(MongoUploadedView);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement