Skip to content

Instantly share code, notes, and snippets.

@qvil
Created August 8, 2021 03:48
Show Gist options
  • Save qvil/51634128561b748829c245f22ea5b9c4 to your computer and use it in GitHub Desktop.
Save qvil/51634128561b748829c245f22ea5b9c4 to your computer and use it in GitHub Desktop.
Example reducer for request, success, failure pattern
const initialState = {
isLoading: false,
data: null,
error: null,
};
const reducer = (state = initialState, action) => {
switch (action.type) {
case "GET_DATA_REQUEST":
return {
...state,
isLoading: true,
data: null,
error: null,
};
case "GET_DATA_SUCCESS":
return {
...state,
isLoading: false,
data: action.data,
error: null,
};
case "GET_DATA_FAILURE":
return {
...state,
isLoading: false,
data: null,
error: action.data,
};
default:
return state;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment