Created
November 3, 2018 15:45
-
-
Save PaquitoSoft/3960a298d0101a8a4d744f381b1a622f to your computer and use it in GitHub Desktop.
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
import { useState, useEffect, useContext } from 'react'; | |
import LoaderContext from '../contexts/loader-context'; | |
export default function useExternalData(loader) { | |
const [externalData, setExternalData] = useState({ | |
data: undefined, | |
isLoading: true, | |
error: undefined | |
}); | |
const context = useContext(LoaderContext); | |
useEffect(() => { | |
context.setIsLoading(true); | |
loader() | |
.then(data => { | |
setExternalData({ | |
...externalData, | |
isLoading: false, | |
data | |
}); | |
context.setIsLoading(false); | |
}) | |
.catch(error => { | |
setExternalData({ | |
...externalData, | |
isLoading: false, | |
error | |
}); | |
context.setIsLoading(false); | |
}); | |
}, []); | |
return externalData; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment