Spaces:
Sleeping
Sleeping
Download frontend/src/hooks/useAsync.js from shield137/shockmap-api: direct link, hf CLI and curl.
- Browser
- Download file 618 Bytes
-
https://huggingface.co/spaces/shield137/shockmap-api/resolve/main/frontend/src/hooks/useAsync.js
- Command line
-
hf download hf://spaces/shield137/shockmap-api/frontend/src/hooks/useAsync.js
-
curl -L -o useAsync.js https://huggingface.co/spaces/shield137/shockmap-api/resolve/main/frontend/src/hooks/useAsync.js
618 Bytes
| import { useState, useEffect, useCallback } from 'react'; | |
| export function useAsync(fn, deps = []) { | |
| const [state, setState] = useState({ data: null, loading: true, error: null }); | |
| const run = useCallback(async () => { | |
| setState(s => ({ ...s, loading: true, error: null })); | |
| try { | |
| const data = await fn(); | |
| setState({ data, loading: false, error: null }); | |
| } catch (e) { | |
| setState({ data: null, loading: false, error: e.message }); | |
| } | |
| // eslint-disable-next-line react-hooks/exhaustive-deps | |
| }, deps); | |
| useEffect(() => { run(); }, [run]); | |
| return { ...state, refetch: run }; | |
| } | |