bigbuffet-rw/app/soapbox/hooks/useLoading.ts

19 lines
417 B
TypeScript
Raw Normal View History

2023-03-23 16:42:34 -07:00
import { useState } from 'react';
function useLoading(initialState: boolean = false) {
const [isLoading, setIsLoading] = useState<boolean>(initialState);
2023-03-23 16:42:34 -07:00
function setPromise<T>(promise: Promise<T>) {
setIsLoading(true);
promise
.then(() => setIsLoading(false))
.catch(() => setIsLoading(false));
return promise;
}
return [isLoading, setPromise] as const;
}
export { useLoading };