Fix useAsyncMemo out-of-order resolutions
This commit is contained in:
parent
02d11b8926
commit
a6ca8f797d
1 changed files with 9 additions and 1 deletions
|
@ -21,7 +21,15 @@ type Fn<T> = () => Promise<T>;
|
||||||
export const useAsyncMemo = <T>(fn: Fn<T>, deps: DependencyList, initialValue?: T): T => {
|
export const useAsyncMemo = <T>(fn: Fn<T>, deps: DependencyList, initialValue?: T): T => {
|
||||||
const [value, setValue] = useState<T>(initialValue);
|
const [value, setValue] = useState<T>(initialValue);
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
fn().then(setValue);
|
let discard = false;
|
||||||
|
fn().then(v => {
|
||||||
|
if (!discard) {
|
||||||
|
setValue(v);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return () => {
|
||||||
|
discard = true;
|
||||||
|
};
|
||||||
}, deps); // eslint-disable-line react-hooks/exhaustive-deps
|
}, deps); // eslint-disable-line react-hooks/exhaustive-deps
|
||||||
return value;
|
return value;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue