Use action

This commit is contained in:
2026-02-05 13:26:04 +01:00
parent b6d137b1d0
commit 07ea424c3e
4 changed files with 187 additions and 149 deletions
+19
View File
@@ -0,0 +1,19 @@
import { useEffect, useState } from "react";
export default function useAction<T>(
action: () => Promise<T>,
dependencies = [],
): [T | null, boolean, Error | null] {
const [value, setValue] = useState<T | null>(null);
const [loaded, setLoaded] = useState(false);
const [error, setError] = useState(null);
useEffect(() => {
action()
.then(setValue)
.catch(setError)
.finally(() => setLoaded(true));
}, dependencies);
return [value, loaded, error];
}