Last active
July 19, 2019 23:32
-
-
Save davidwallacejackson/e26aa8c10fb042cbb161b06c87b2abc0 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 } from 'react'; | |
| type State<T> = { | |
| data?: T; | |
| error?: T; | |
| }; | |
| export default function useAsync<T>( | |
| getData: () => Promise<T>, | |
| deps: readonly any[], | |
| ): State<T> { | |
| const [state, setState] = useState<State<T>>({}); | |
| useEffect(() => { | |
| getData() | |
| .then(data => setState({ data })) | |
| .catch(error => setState({ error })); | |
| }, deps); | |
| return state; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment