1.0.1 • Published 1 year ago
ikeyit-react-easy v1.0.1
Install
npm install ikeyit-react-easy
Quickstart
useAsync
The hook is used to track status of an async action. When the action starts, the status becomes to be "loading". You
can show a loading indicator or disable some UI components. When the action is finished successfully, the status becomes to
be "success". You can show the "data". If the action fails, the status will be "error". You can toast the error to the user.
If the action is executed many times simultaneously, only the latest data is returned.
import { useAsync } from 'ikeyit-react-easy';
function YourComponent() {
const {data, error, status, execute} = useAsync(
params => asyncMethod(params),
{
onSuccess: () => {},
onError: () => {}
}
);
return (
<>
<div>status: {status}</div>
<div>data: {data}</div>
<div>error: {error}</div>
<button onClick={e => execute()}>Run slow async action</button>
</>
);
}