Skip to content

Commit 625d7ae

Browse files
authored
update day-21 code snippets
1 parent cc7c5dc commit 625d7ae

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

day-21/post.md

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,11 @@ Let's create our first middleware.
111111
Now the signature of middleware looks like this:
112112

113113
```javascript
114+
// src/redux/loggingMiddleWare.js
114115
const loggingMiddleware = (store) => (next) => (action) => {
115116
// Our middleware
116117
}
118+
export default loggingMiddleware;
117119
```
118120

119121
Befuddled about this middleware thing? Don't worry, we all are the first time we see it. Let's peel it back a little bit and destructure what's going on. That `loggingMiddleware` description above could be rewritten like the following:
@@ -157,14 +159,16 @@ import { createStore, applyMiddleware } from 'redux';
157159
To _apply_ middleware, we can call this `applyMiddleware()` function in the `createStore()` method. In our `src/redux/configureStore.js` file, let's update the store creation by adding a call to `applyMiddleware()`:
158160

159161
```javascript
160-
const store = createStore(
161-
rootReducer,
162-
initialState,
163-
applyMiddleware(
164-
apiMiddleware,
165-
loggingMiddleware,
166-
)
167-
);
162+
// ...
163+
import loggingMiddleware from "./loggingMiddleware";
164+
// ...
165+
const store = createStore(
166+
rootReducer,
167+
initialState,
168+
applyMiddleware(
169+
loggingMiddleware,
170+
)
171+
);
168172
```
169173

170174
Now our middleware is in place. Open up the console in your browser to see all the actions that are being called for this demo. Try clicking on the `Update` button with the console open...
@@ -176,24 +180,26 @@ As we've seen, middleware gives us the ability to insert a function in our Redux
176180
We want to write a middleware function that can handle API requests. We can write a middleware function that listens only to actions corresponding to API requests. Our middleware can "watch" for actions that have a special marker. For instance, we can have a `meta` object on the action with a `type` of `'api'`. We can use this to ensure our middleware does not handle any actions that are not related to API requests:
177181

178182
```javascript
183+
// src/redux/apiMiddleware.js
179184
const apiMiddleware = store => next => action => {
180185
if (!action.meta || action.meta.type !== 'api') {
181186
return next(action);
182187
}
183188

184189
// This is an api request
185190
}
191+
export default apiMiddleware
186192
```
187193

188194
If an action does have a meta object with a type of `'api'`, we'll pick up the request in the `apiMiddleware`.
189195

190-
Let's convert our `updateTime()` actionCreator to include these properties into an API request. Let's open up the `currentTime` redux module we've been working with (in `src/redux/currentTime.js`) and find the `fetchNewTime()` function definition.
196+
Let's convert our `fetchNewTime()` actionCreator to include these properties into an API request. Let's open up the `actionCreators` redux module we've been working with (in `src/redux/actionCreators.js`) and find the `fetchNewTime()` function definition.
191197

192198
Let's pass in the URL to our `meta` object for this request. We can even accept parameters from inside the call to the action creator:
193199

194200
```javascript
195201
const host = 'https://andthetimeis.com'
196-
export const fetchNewTime = ({ timezone = 'pst', str='now'}) => ({
202+
export const fetchNewTime = (timezone = 'pst', str='now') => ({
197203
type: types.FETCH_NEW_TIME,
198204
payload: new Date().toString(),
199205
meta: {

0 commit comments

Comments
 (0)