Skip to content

Commit 9ab269e

Browse files
authored
update day-17 code snippets
1 parent a2f3177 commit 9ab269e

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

day-20/post.md

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ As a refresher, an action is a simple object that _must_ include a `type` value.
4040

4141
```javascript
4242
export const FETCH_NEW_TIME = 'FETCH_NEW_TIME';
43+
export const LOGIN = 'USER_LOGIN';
44+
export const LOGOUT = 'USER_LOGOUT';
4345
```
4446

4547
As a quick review, our actions can be any object value that has the `type` key. We can send data along with our action (conventionally, we'll pass extra data along as the `payload` of an action).
@@ -71,15 +73,6 @@ export const fetchNewTime = () => ({
7173
type: types.FETCH_NEW_TIME,
7274
payload: new Date().toString(),
7375
})
74-
75-
export const login = (user) => ({
76-
type: types.LOGIN,
77-
payload: user
78-
})
79-
80-
export const logout = () => ({
81-
type: types.LOGOUT,
82-
})
8376
```
8477

8578
Now if we call this function, _nothing_ will happen except an action object is returned. How do we get this action to dispatch on the store?
@@ -89,7 +82,7 @@ Recall we used the `connect()` function export from `react-redux` yesterday? The
8982
Let's see this in action. In our `src/views/Home/Home.js` file, let's update our call to connect by providing a second function to use the actionCreator we just created. We'll call this function `mapDispatchToProps`.
9083

9184
```javascript
92-
import { fetchNewTime } from '../../redux/actionCreators';
85+
import { fetchNewTime } from '../../../redux/actionCreators';
9386
// ...
9487
const mapDispatchToProps = dispatch => ({
9588
updateTime: () => dispatch(fetchNewTime())
@@ -190,6 +183,19 @@ export const configureStore = () => {
190183
export default configureStore;
191184
```
192185

186+
Let's also update our `Home` component `mapStateToProps` function to read it's value from the `time` reducer
187+
188+
```javascript
189+
// ...
190+
const mapStateToProps = state => {
191+
// our redux store has `time` and `user` states
192+
return {
193+
currentTime: state.time.currentTime
194+
};
195+
};
196+
// ...
197+
```
198+
193199
Now we can create the `login()` and `logout()` action creators to send along the action on our store.
194200

195201
```javascript

0 commit comments

Comments
 (0)