|
| 1 | +##### How exactly does mapStateToProps know which is the state variable that I am talking about. In other words, when I have many reducers file how each component, will get its specific states from mapStateToProps() |
| 2 | + |
| 3 | +Lets say I have a module (among many ) called Tidal in my App, that will get and render tidal data. |
| 4 | + |
| 5 | +##### So here's part of the ../actions/tidalActions.js file (There will be many other action file as well for all other modules of the app) |
| 6 | + |
| 7 | +```js |
| 8 | +import { |
| 9 | + FETCH_ALL_TIDES, |
| 10 | + FETCH_CURRENT_TIDES, |
| 11 | + FETCH_CURRENT_WEEK_TIDES |
| 12 | +} from "./types"; |
| 13 | + |
| 14 | +export const getAllTides = () => async dispatch => { |
| 15 | + const res = await axios.get("/api/tidal/alltides"); |
| 16 | + dispatch({ type: FETCH_ALL_TIDES, payload: res.data }); |
| 17 | +}; |
| 18 | + |
| 19 | +export const getCurrentDateTides = () => async dispatch => { |
| 20 | + const res = await axios.get("/api/tidal/currentdate"); |
| 21 | + dispatch({ type: FETCH_CURRENT_TIDES, payload: res.data }); |
| 22 | +}; |
| 23 | + |
| 24 | +export const getCurrentWeekTides = () => async dispatch => { |
| 25 | + const res = await axios.get("/api/tidal/currentweek"); |
| 26 | + dispatch({ type: FETCH_CURRENT_WEEK_TIDES, payload: res.data }); |
| 27 | +}; |
| 28 | +``` |
| 29 | + |
| 30 | +##### Here's part of the reducer file - ../reducers/tidalReducers.js - (there will be many other reducers for the other modules of the app) |
| 31 | + |
| 32 | +```js |
| 33 | +import { |
| 34 | + FETCH_CURRENT_TIDES, |
| 35 | + FETCH_CURRENT_WEEK_TIDES, |
| 36 | + FETCH_ALL_TIDES |
| 37 | +} from "../actions/types"; |
| 38 | + |
| 39 | +const initialState = { |
| 40 | + list: [], |
| 41 | + currentDate: [], |
| 42 | + currentWeek: [] |
| 43 | +}; |
| 44 | + |
| 45 | +export default function(state = initialState, actions) { |
| 46 | + switch (actions.type) { |
| 47 | + case FETCH_ALL_TIDES: |
| 48 | + return { ...state, list: actions.payload }; |
| 49 | + case FETCH_CURRENT_TIDES: |
| 50 | + return { ...state, currentDate: actions.payload }; |
| 51 | + case FETCH_CURRENT_WEEK_TIDES: |
| 52 | + return { ...state, currentWeek: actions.payload }; |
| 53 | + default: |
| 54 | + return state; |
| 55 | + } |
| 56 | +} |
| 57 | +``` |
| 58 | + |
| 59 | +##### And finally here the root-reducer file - ../reducers/index.js - which will return all the reducers file's results through the combineReducers() function. |
| 60 | + |
| 61 | +```js |
| 62 | +import { combineReducers } from "redux"; |
| 63 | +import galleryReducers from "./galleryReducers"; |
| 64 | +import tidalReducers from "./tidalReducers"; |
| 65 | +import documentReducers from "./documentReducers"; |
| 66 | +import tariffReducers from "./tariffReducers"; |
| 67 | +import projectReducers from "./projectReducers"; |
| 68 | + |
| 69 | +export default combineReducers({ |
| 70 | + tides: tidalReducers, |
| 71 | + |
| 72 | + documents: documentReducers, |
| 73 | + |
| 74 | + gos: goReducers, |
| 75 | + |
| 76 | + tariffs: tariffReducers, |
| 77 | + |
| 78 | + gallery: galleryReducers, |
| 79 | + |
| 80 | + project: projectReducers |
| 81 | +}); |
| 82 | +``` |
| 83 | + |
| 84 | +See, that for each of the reducers I am returning the state object specifying a single variable as the key. So for the tidalReducers, I am returning the key as 'tides' |
| 85 | + |
| 86 | +#### And thats the most important point |
| 87 | + |
| 88 | +Now in the relevant tidal component - say its name is TidalList.js I need to have access to the to the three states from store, that tidalReducers is returning |
| 89 | + |
| 90 | +``` |
| 91 | +const initialState = { |
| 92 | +list: [], |
| 93 | +currentDate: [], |
| 94 | +currentWeek: [] |
| 95 | +}; |
| 96 | +``` |
| 97 | + |
| 98 | +SO I PULL IN THE 'tides' VARIABLE WITH mapStateToProps() as below - NOTE, I HAVE TO HAVE THE ARGUMENT OF mapStateToProps() withing the curly braces to match exactly what the root-reducer returned inside combineReducers function - inside ../reducers/index.js file |
| 99 | + |
| 100 | +```js |
| 101 | +const mapStateToProps = ({ tides }) => { |
| 102 | + return { tides: tides.currentWeek }; |
| 103 | +}; |
| 104 | + |
| 105 | +const mapDispatchToProps = dispatch => { |
| 106 | + return { |
| 107 | + getAllCurrentWeekTidesDispatch: () => { |
| 108 | + dispatch(getCurrentWeekTides()); |
| 109 | + }, |
| 110 | + deleteTide, |
| 111 | + dispatch |
| 112 | + }; |
| 113 | +}; |
| 114 | + |
| 115 | +TidalsList.propTypes = { |
| 116 | + tides: PropTypes.array.isRequired, |
| 117 | + classes: PropTypes.object.isRequired |
| 118 | +}; |
| 119 | + |
| 120 | +export default connect( |
| 121 | + mapStateToProps, |
| 122 | + mapDispatchToProps |
| 123 | +)(withStyles(styles)(TidalsList)); |
| 124 | +``` |
0 commit comments