如何在同一页面的React-native中从多个api端点获取数据

问题描述 投票:-2回答:1

我正在尝试从两个不同的api端点获取数据,并试图在我的React本机应用程序This is the snapshot of the error I'm getting with the code snippet中的同一页面中显示整个数据。>

我正在尝试从两个不同的api端点获取数据,并试图在我的React本机应用程序的同一页面中显示整个数据。这是我通过代码片段得到的错误的快照

react-native fetch datasource
1个回答
1
投票
[同样的异常对我也来了...尝试了一种不同的方法。将一种获取方法绑定到另一种获取方法中,并尝试将值设置到第一个获取块中的所有数据源变量中,然后在下一个获取中使用第二个变量再次获取块。

0
投票
fetch(‘endpoint1’, { method: 'post', headers: { 'Accept': 'application/json', 'Content-type': 'application/json' }, body: JSON.stringify({ //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); });
© www.soinside.com 2019 - 2024. All rights reserved.