Created
October 17, 2018 16:51
-
-
Save jepras/81cb00f1b7cab19906f1af5a772d66f7 to your computer and use it in GitHub Desktop.
Create action dispatchers requestingData & received data. These feed into the handleAsync asynchronous function that first dispatches the first, and after 2,5 dispatches the second. Also added Redux.applyMiddleware(ReduxThunk.default) as second parameter to store variable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
dispatch(requestingData()); | |
setTimeout(function() { | |
let data = { | |
users: ['Jeff', 'William', 'Alice'] | |
} | |
// dispatch received data action here | |
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) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment