Reactjs api获取请求失败后,在另一个异步函数内部重新调用useEffect中的异步函数
这有点难以解释,但以下是我正在做的:Reactjs api获取请求失败后,在另一个异步函数内部重新调用useEffect中的异步函数,reactjs,react-native,fetch-api,use-effect,Reactjs,React Native,Fetch Api,Use Effect,这有点难以解释,但以下是我正在做的: 尝试从名为getJsonData()的异步函数获取json数据,直到获取数据为止 正确获取数据后,我想从getOtherJsonData()获取另一组json数据 下面的代码可以正确地获取第一组数据(getJsonData),即使在X失败之后也是如此。(如有的话) 但是,它不会一直获取第二组数据(getOtherJsonData),因为可能会发生错误。我希望继续重新执行下面标记的代码块,直到第二组数据正确返回 ... import React, {useSt
...
import React, {useState, useEffect} from 'react';
import {getJsonData} from './getJsonData';
imoport {getOtherJsonData} from './getOtherJsonData';
const myApp = () => {
const [errorFetchedChecker, setErrorFetchedChecker] = useState(false);
const [isLoading,setIsLoading] = useState(true);
const [data,setData] = useState(null);
const updateState = jsonData => {
setIsloading(false);
setData(jsonData);
};
useEffect(() => {
getJsonData().then(
data => {
updateState(data);
// This is the bloc I want to keep re-executing
//
getOtherJsonData(data.title).then(
otherData => {
updateOtherState(otherData);
console.log("Updated with no error);
},
otherError => {
console.log("Error, try getOtherJsonData again ?");
console.log("Can't try to refresh, no errorFetchedChecker for me :/ ");
}
//
// Until It doesn't return an error
},
error => {
console.log('Error fetching, re-trying to fetch thanks to errorFetchedChecker');
setErrorFetchedChecker(c => !c);
},
);
}, [errorFetchedChecker]);
return (
<View>
<Text>{state.data.title}</Text>
<Text>{data.data.completed}</Text>
</View>
);
}
这是我的另一个问题,它解释了在出现故障时如何重新执行第一个getJsonData()
以下是我尝试过但在未处理的承诺方面给了我错误的东西:
const subFunction(myTitle) => {
getOtherJsonData(myTitle).then(
otherData => {
updateOtherState(otherData);
console.log("Updated with no error);
},
otherError => {
console.log("Error, try getOtherJsonData again!");
subFunction(myTitle); //Gives Unhandled promise warning and no result
}
}
useEffect(() => {
getJsonData().then(
data => {
updateState(data);
// This is the bloc I want to keep re-executing
//
subFunction(data.title);
//
// Until It doesn't return an error
},
error => {
console.log('Error fetching, re-trying to fetch thanks to errorFetchedChecker');
setErrorFetchedChecker(c => !c);
},
);
}, [errorFetchedChecker]);
注意:请随意以任何方式、形状或形式重新表述标题。您可以尝试使用两个
useffect
来分离这两个函数,因为现在您必须重复第一个请求,以防第二次失败。大概是这样的:
useEffect(() => {
getJsonData().then(
data => {
updateState(data);
},
error => {
console.log('Error fetching, re-trying to fetch thanks to errorFetchedChecker');
setErrorFetchedChecker(c => !c);
},
);
}, [errorFetchedChecker]);
useEffect(() => {
// prevent request if there's no data
if (data) {
getOtherJsonData(data.title).then(
otherData => {
updateOtherState(otherData);
console.log("Updated with no error);
},
otherError => {
console.log("Error, try getOtherJsonData again ?");
console.log("Can't try to refresh, no errorFetchedChecker for me :/ ");
// you'll have to create one more state for that
setOtherErrorFetchedChecker(c => !c);
}
}
}, [data, otherErrorFetchedChecker])
我只是想再核实一下,这是你的原始代码吗?它有几个未终止的字符串。它的总体代码是相同的,只是删除了一部分并进行了重命名以避免混淆。工作流畅,谢谢!快速提问:您建议如何尝试X次而不是无限期地提取数据?@AnisBenna您可以将
otherErrorFetchedChecker
设置为整数而不是布尔值。默认设置为零。例如,if语句可以是if(data&&otherErrorFetchedChecker<5)
。如果请求失败setOtherErrorFetchedChecker(c=>c++)
useEffect(() => {
getJsonData().then(
data => {
updateState(data);
},
error => {
console.log('Error fetching, re-trying to fetch thanks to errorFetchedChecker');
setErrorFetchedChecker(c => !c);
},
);
}, [errorFetchedChecker]);
useEffect(() => {
// prevent request if there's no data
if (data) {
getOtherJsonData(data.title).then(
otherData => {
updateOtherState(otherData);
console.log("Updated with no error);
},
otherError => {
console.log("Error, try getOtherJsonData again ?");
console.log("Can't try to refresh, no errorFetchedChecker for me :/ ");
// you'll have to create one more state for that
setOtherErrorFetchedChecker(c => !c);
}
}
}, [data, otherErrorFetchedChecker])