Created
August 8, 2021 03:48
-
-
Save qvil/51634128561b748829c245f22ea5b9c4 to your computer and use it in GitHub Desktop.
Example reducer for request, success, failure pattern
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 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