mirror of
https://github.com/apricote/Listory.git
synced 2026-01-13 21:21: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>(
|
type UseAsync = <T>(
|
||||||
asyncFunction: () => Promise<T>,
|
asyncFunction: () => Promise<T>,
|
||||||
|
|
@ -17,19 +17,23 @@ export const useAsync: UseAsync = <T extends any>(
|
||||||
const [pending, setPending] = useState(false);
|
const [pending, setPending] = useState(false);
|
||||||
const [value, setValue] = useState<T>(initialValue);
|
const [value, setValue] = useState<T>(initialValue);
|
||||||
const [error, setError] = useState(null);
|
const [error, setError] = useState(null);
|
||||||
|
const [, startTransition] = useTransition();
|
||||||
|
|
||||||
// The execute function wraps asyncFunction and
|
// The execute function wraps asyncFunction and
|
||||||
// handles setting state for pending, value, and error.
|
// handles setting state for pending, value, and error.
|
||||||
// useCallback ensures the below useEffect is not called
|
// useCallback ensures the below useEffect is not called
|
||||||
// on every render, but only if asyncFunction changes.
|
// on every render, but only if asyncFunction changes.
|
||||||
const execute = useCallback(() => {
|
const execute = useCallback(() => {
|
||||||
setPending(true);
|
startTransition(() => {
|
||||||
setValue(initialValue);
|
setPending(true);
|
||||||
setError(null);
|
setValue(initialValue);
|
||||||
|
setError(null);
|
||||||
|
});
|
||||||
|
|
||||||
return asyncFunction()
|
return asyncFunction()
|
||||||
.then((response) => setValue(response))
|
.then((response) => startTransition(() => setValue(response)))
|
||||||
.catch((err) => setError(err))
|
.catch((err) => startTransition(() => setError(err)))
|
||||||
.finally(() => setPending(false));
|
.finally(() => startTransition(() => setPending(false)));
|
||||||
}, [asyncFunction, initialValue]);
|
}, [asyncFunction, initialValue]);
|
||||||
|
|
||||||
// Call execute if we want to fire it right away.
|
// Call execute if we want to fire it right away.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue