Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jepras/81cb00f1b7cab19906f1af5a772d66f7 to your computer and use it in GitHub Desktop.
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
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