从请求中获取的数据具有
price
在下面
item.quotes.UDS.price
,不是
item.quotes.price
是的。
还要确保初始化空的
dataSource
您所在州的阵列:
class FetchExample extends React.Component {
constructor(props) {
super(props);
this.state = { isLoading: true, dataSource: [] };
}
componentDidMount() {
return fetch("https://api.coinmarketcap.com/v2/ticker/?start=1&limit=10&sort=id&structure=array")
.then(response => response.json())
.then(responseJson => {
this.setState({
isLoading: false,
dataSource: responseJson.data
});
});
}
render() {
if (this.state.isLoading) {
return (
<View style={{ flex: 1, padding: 20 }}>
<ActivityIndicator />
</View>
);
}
return (
<View style={{ flex: 1, paddingTop: 20 }}>
<FlatList
data={this.state.dataSource}
renderItem={({ item }) => (
<Text>
{item.name}, {item.symbol}, {item.quotes.USD.price}
</Text>
)}
keyExtractor={(item, index) => index}
/>
</View>
);
}
}