Skip to content

Instantly share code, notes, and snippets.

@tpjnorton
Created October 23, 2020 08:55
Show Gist options
  • Save tpjnorton/ca4a66c4981691e8e7c0900a3248b8a9 to your computer and use it in GitHub Desktop.
Save tpjnorton/ca4a66c4981691e8e7c0900a3248b8a9 to your computer and use it in GitHub Desktop.
useLoading.ts
import { useState } from "react";
const useLoading = (action) => {
const [loading, setLoading] = useState(false);
const doAction = (...args) => {
setLoading(true);
return action(...args).finally(() => setLoading(false));
};
return [doAction, loading];
};
export default useLoading;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment