Hello all!
I’m trying to learn Redux but this code from the “Middleware” section of the challenge confuses me. I’ve tried looking it up to find what it means but nothing comes close. Here’s the code from the last line for easy reference:
const store = Redux.createStore( asyncDataReducer, Redux.applyMiddleware(ReduxThunk.default) )
My question is, what is “ReduxThunk.default”, and how does it affect the code in the challenge?
My other question is; since we are supposed to create the store once, do we include Redux Thunk Middleware by default so that the entire redux code could use asynchronous programming?
Any form of help is appreciated!
const REQUESTING_DATA = 'REQUESTING_DATA'
const RECEIVED_DATA = 'RECEIVED_DATA'
const requestingData = () => { return {type: REQUESTING_DATA} }
const receivedData = (data) => { return {type: RECEIVED_DATA, users: data.users} }
const handleAsync = () => {
return function(dispatch) {
// Dispatch request action here
store.dispatch(requestingData())
setTimeout(function() {
let data = {
users: ['Jeff', 'William', 'Alice']
}
// Dispatch received data action here
store.dispatch(receivedData(data))
}, 2500);
}
};
const defaultState = {
fetching: false,
users: []
};
const asyncDataReducer = (state = defaultState, action) => {
switch(action.type) {
case REQUESTING_DATA:
return {
fetching: true,
users: []
}
case RECEIVED_DATA:
return {
fetching: false,
users: action.users
}
default:
return state;
}
};
const store = Redux.createStore(
asyncDataReducer,
Redux.applyMiddleware(ReduxThunk.default)
);
**Your browser information:**
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36
Challenge: Use Middleware to Handle Asynchronous Actions
Link to the challenge: