[英]How can I fetch data from multiple api endpoints in React-native in the same page
The same exception was coming for me as well...tried a different approach.Chained one fetch method in another and tried to set the value into all the datasource variables in the first fetch block and then used the 2nd variable in the next fetch block again.同样的异常也出现在我身上......尝试了一种不同的方法。在另一个方法中链接一个获取方法并尝试将值设置到第一个获取块中的所有数据源变量中,然后在下一个获取块中使用第二个变量再次。
The issue got resolved and the values are now coming as required.问题得到解决,值现在按要求出现。 Don't know if this is the perfect approach but it solved the issue, please try to refer the below example不知道这是否是完美的方法,但它解决了问题,请尝试参考以下示例
fetch(‘endpoint1’, {
method: 'post',
headers: {
'Accept': 'application/json',
'Content-type': 'application/json'
},
body: JSON.stringify({
//send the parameters you need to use to fetch the data
from endpoint
//e.g. “id”: this.state.id,
…
})
})
.then((response) => response.json())
.then((responseJson) => {
this.setState({
isLoading: false,
dataSource1: responseJson,
dataSource2: responseJson,
})
})
.then(()=>{
fetch(‘endpoint2', {
method: 'post',
headers: {
'Accept': 'application/json',
'Content-type': 'application/json'
},
body: JSON.stringify({
//send the parameters you need to use to fetch the data
from endpoint
//e.g. “id”: this.state.id,
…
})
})
.then((response) => response.json())
.then((responseJson) => {
this.setState({
isLoading: false,
dataSource2: responseJson,
})
})
})
.catch((error) => {
console.error(error);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.