Created
February 14, 2020 17:53
-
-
Save tudorilisoi/81aefc0105b9074259923d871e46a8ad to your computer and use it in GitHub Desktop.
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
function Async({promiseFn, fallback, errFallback, children}){ | |
const [state, setState] = useState('initial') | |
useEffect(()=>{ | |
promiseFn().then(()=>{ | |
setState('resolved') | |
}).catch(()=>{ | |
setState('error') | |
}) | |
}) | |
switch (state) { | |
case 'initial': | |
return fallback; | |
break; | |
case 'resolved': | |
return <>{children}</>; | |
break; | |
default: | |
break; | |
} | |
} | |
//usage | |
<Async promiseFn={()=>loadImages()}> | |
<ImagesPage /> | |
</Async> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment