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