Skip to content

Commit a8954a9

Browse files
committed
mapStateToProps - how it accesses the states from store
1 parent fc50c7a commit a8954a9

2 files changed

+141
-4
lines changed

Redux/mapStateToProps-basic-understanding-2.md

+17-4
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ ShoppingList.PropTypes = {
3131
};
3232
```
3333

34-
what mapStateToProps() does is, it allows us to take our item state (from itemReducer.js ) and turn this into a component property so I can use it in this ShoppingList component - like e.g.
35-
this.props.items
34+
what mapStateToProps() does is, it allows us to take our item state (from itemReducer.js ) and turn this into a component property so I can use it in this ShoppingList component - like e.g. `this.props.items`
35+
3636
mapStateToProps() has the Store state as an argument and its used to link the component with certain part of the store state . In returned object from mapStateToProps() below, I am using 'item' as key because thats what I am calling it in my rootReducer (./reducers/index.js)
3737

3838
```js
@@ -52,7 +52,20 @@ export default connect(
5252
)(ShoppingList);
5353
```
5454

55-
mapStateToProps and mapDispatchToProps are both pure functions that are provided with the (or passed to them), the stores “state” and “dispatch” respectively. Furthermore, both functions have to return an object, whose keys will then be passed on as the props of the component they are connected to (ShoppingList in this case).
55+
mapStateToProps and mapDispatchToProps are both pure functions that are provided with (or passed to them), the stores “state” and “dispatch” respectively. Furthermore, both functions have to return an object, whose keys will then be passed on as the props of the component they are connected to (ShoppingList in this case).
56+
57+
#### MOST IMPORTANT POINT (THAT BAFFLED ME INITIALLY) - How dows mapStateToProps know which is this 'item' variable that I am talking about.
58+
59+
##### And the answer is from the rootReducer (./reducers/index.js) file. There, from combineReducers() functions I am returning all the state that ./itemReducer.js file is returning. by doing this
60+
61+
```js
62+
import { combineReducers } from "redux";
63+
import itemReducer from "./itemReducer";
64+
65+
export default combineReducers({
66+
item: itemReducer
67+
});
68+
```
5669

5770
## 2. mapStateToProps - General Syntax - 2nd Example
5871

@@ -109,7 +122,7 @@ On the other hand, when we want to retrieve data, we do not get it directly from
109122

110123
This is precisely what connect does. It maps the store's state and dispatch to the props of a component :
111124

112-
mapStateToProps and mapDispatchToProps are both pure functions that are provided with the (or passed to them), stores “state” and “dispatch” respectively. Furthermore, both functions have to return an object, whose keys will then be passed on as the props of the component they are connected to (ShoppingList in this case).
125+
**mapStateToProps** and **mapDispatchToProps** are both pure functions that are provided with the (or passed to them), stores “state” and “dispatch” respectively. Furthermore, both functions have to return an object, whose keys will then be passed on as the props of the component they are connected to (ShoppingList in this case).
113126

114127
In this case, mapStateToProps returns an object with only one key : “item”.
115128

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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

Comments
 (0)