Created
April 7, 2019 21:23
-
-
Save michalczaplinski/dfd008439c0f6eb5d251e58a2b9cab37 to your computer and use it in GitHub Desktop.
use loading hook
This file contains hidden or 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
| 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