Small improvements:
- Switch to CodeMirror - Switch to Parcel Bundler - Fix synchronisation bug - Update dependencies
This commit is contained in:
23
src/hooks.ts
Normal file
23
src/hooks.ts
Normal file
@ -0,0 +1,23 @@
|
||||
import { useEffect, useMemo, useState } from "preact/hooks";
|
||||
|
||||
export function usePromise<T>(promiseFnc: () => Promise<T>, params: any[]) {
|
||||
const promise = useMemo(promiseFnc, params);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState(undefined);
|
||||
const [value, setValue] = useState(undefined);
|
||||
|
||||
useEffect(() => {
|
||||
let canceled = false;
|
||||
|
||||
promise
|
||||
.then((res) => setValue(res))
|
||||
.catch((err) => setError(err))
|
||||
.finally(() => setLoading(false));
|
||||
|
||||
return () => {
|
||||
canceled = true;
|
||||
};
|
||||
}, [promise]);
|
||||
|
||||
return [loading, error, value];
|
||||
}
|
Reference in New Issue
Block a user