Skip to content

Instantly share code, notes, and snippets.

@ZephyrBlu
Created February 25, 2021 12:49
Show Gist options
  • Save ZephyrBlu/1d9d0b1c5c6e9025b011bf80a1de04a6 to your computer and use it in GitHub Desktop.
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
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