1
0
mirror of https://github.com/matrix-org/matrix-react-sdk.git synced 2025-11-07 10:46:24 +03:00

Fix useAsyncMemo out-of-order resolutions

This commit is contained in:
Michael Telatynski
2021-05-25 15:34:44 +01:00
parent 02d11b8926
commit a6ca8f797d

View File

@@ -21,7 +21,15 @@ type Fn<T> = () => Promise<T>;
export const useAsyncMemo = <T>(fn: Fn<T>, deps: DependencyList, initialValue?: T): T => {
const [value, setValue] = useState<T>(initialValue);
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
return value;
};