Reset error/result only after the next request has resolved to prevent render flicking

This commit is contained in:
mabashian 2020-09-29 13:11:06 -04:00
parent 2019f808b9
commit f672cee3a0

View File

@ -38,18 +38,16 @@ export default function useRequest(makeRequest, initialValue) {
request: useCallback(
async (...args) => {
setIsLoading(true);
if (isMounted.current) {
setResult(initialValue);
setError(null);
}
try {
const response = await makeRequest(...args);
if (isMounted.current) {
setResult(response);
setError(null);
}
} catch (err) {
if (isMounted.current) {
setError(err);
setResult(initialValue);
}
} finally {
if (isMounted.current) {
@ -57,7 +55,6 @@ export default function useRequest(makeRequest, initialValue) {
}
}
},
/* eslint-disable-next-line react-hooks/exhaustive-deps */
[makeRequest]
),
setValue: setResult,