|
Download react-use/docs/useAsyncFn.md from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 807 Bytes
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-use/docs/useAsyncFn.md
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset@1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-use/docs/useAsyncFn.md
-
curl -L -o useAsyncFn.md https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-use/docs/useAsyncFn.md
807 Bytes
useAsyncFn
React hook that returns state and a callback for an async function or a
function that returns a promise. The state is of the same shape as useAsync.
Usage
import {useAsyncFn} from 'react-use';
const Demo = ({url}) => {
const [state, doFetch] = useAsyncFn(async () => {
const response = await fetch(url);
const result = await response.text();
return result
}, [url]);
return (
<div>
{state.loading
? <div>Loading...</div>
: state.error
? <div>Error: {state.error.message}</div>
: <div>Value: {state.value}</div>
}
<button onClick={() => doFetch()}>Start loading</button>
</div>
);
};
Reference
useAsyncFn<Result, Args>(fn, deps?: any[], initialState?: AsyncState<Result>);