Skip to content

Instantly share code, notes, and snippets.

@michalczaplinski
Created April 7, 2019 21:23
Show Gist options
  • Select an option

  • Save michalczaplinski/dfd008439c0f6eb5d251e58a2b9cab37 to your computer and use it in GitHub Desktop.

Select an option

Save michalczaplinski/dfd008439c0f6eb5d251e58a2b9cab37 to your computer and use it in GitHub Desktop.
use loading hook
export default function useLoading() {
const [loadingState, setLoadingState] = React.useState({
isLoading: true,
hasError: false
});
const loadPromise = aPromise => {
setLoadingState({ isLoading: true, hasError: false });
return aPromise
.then((...args) => {
setLoadingState({ isLoading: false, hasError: false });
return Promise.resolve(...args);
})
.catch((...args) => {
setLoadingState({ isLoading: false, hasError: true });
return Promise.reject(...args);
});
};
return { loadingState, setLoadingState, loadPromise };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment