mirror of
https://github.com/apricote/Listory.git
synced 2026-01-13 13:11:02 +00:00
feat(frontend): use transition for showing new data
This commit is contained in:
parent
4758338e99
commit
0c4de5d56a
1 changed files with 11 additions and 7 deletions
|
|
@ -1,4 +1,4 @@
|
|||
import { useCallback, useEffect, useState } from "react";
|
||||
import { useCallback, useEffect, useState, useTransition } from "react";
|
||||
|
||||
type UseAsync = <T>(
|
||||
asyncFunction: () => Promise<T>,
|
||||
|
|
@ -17,19 +17,23 @@ export const useAsync: UseAsync = <T extends any>(
|
|||
const [pending, setPending] = useState(false);
|
||||
const [value, setValue] = useState<T>(initialValue);
|
||||
const [error, setError] = useState(null);
|
||||
const [, startTransition] = useTransition();
|
||||
|
||||
// The execute function wraps asyncFunction and
|
||||
// handles setting state for pending, value, and error.
|
||||
// useCallback ensures the below useEffect is not called
|
||||
// on every render, but only if asyncFunction changes.
|
||||
const execute = useCallback(() => {
|
||||
setPending(true);
|
||||
setValue(initialValue);
|
||||
setError(null);
|
||||
startTransition(() => {
|
||||
setPending(true);
|
||||
setValue(initialValue);
|
||||
setError(null);
|
||||
});
|
||||
|
||||
return asyncFunction()
|
||||
.then((response) => setValue(response))
|
||||
.catch((err) => setError(err))
|
||||
.finally(() => setPending(false));
|
||||
.then((response) => startTransition(() => setValue(response)))
|
||||
.catch((err) => startTransition(() => setError(err)))
|
||||
.finally(() => startTransition(() => setPending(false)));
|
||||
}, [asyncFunction, initialValue]);
|
||||
|
||||
// Call execute if we want to fire it right away.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue