Created
February 25, 2021 12:49
-
-
Save ZephyrBlu/1d9d0b1c5c6e9025b011bf80a1de04a6 to your computer and use it in GitHub Desktop.
Custom hook that can be used in conjunction with the LoadingState component to declaratively manage render state
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
import { useState } from 'react'; | |
const useLoadingState = () => { | |
const [loadingState, _setLoadingState] = useState(null); | |
const allowedStates = [ | |
'inProgress', | |
'success', | |
'error', | |
'notFound', | |
]; | |
const setLoadingState = (state) => { | |
if (!allowedStates.includes(state)) { | |
throw `${state} is an undefined loading state`; | |
} | |
_setLoadingState({ [state]: true }); | |
}; | |
return [ | |
loadingState, | |
setLoadingState, | |
]; | |
}; | |
export default useLoadingState; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment