简体   繁体   English

需要帮助来获取 API

[英]Need help to fetch API

I'm trying to fetch an API but the data are not rendering on my page.我正在尝试获取 API,但数据未在我的页面上呈现。 I think I'm doing something wrong.我想我做错了什么。 Can some help me, I added a photo of the data to fetch and my code also enter image description here可以帮我一下吗,我添加了一张要获取的数据照片,我的代码也在此处输入了图像描述

 export default class CryptoCard extends Component { constructor(props) { super(props) this.state = { cryptos: [], name: props.name, symbol : props.symbol } } componentDidMount() { fetch(`https://api.nomics.com/v1/currencies/ticker?key=${APP_KEY}&ids=BTC,ETH,XRP&attributes=id,name,symbol,price`) .then(response => response.json()) .then(data => console.log(data)) } render() { const { cryptos } = this.state; return ( <div> <ul> { cryptos.map(crypto => <li>{cryptos}</li>) } </ul> </div> ) } }

You need to update your cryptos array once you get back the data from the API like this像这样从 API 取回数据后,您需要更新cryptos数组

 componentDidMount() {
     fetch(`https://api.nomics.com/v1/currencies/ticker?key=${APP_KEY}&ids=BTC,ETH,XRP&attributes=id,name,symbol,price`)
     .then(response => response.json())
     .then(data => this.setState({cryptos:data})
 }

Also in your map here is how to display the info同样在您的地图中,这里是如何显示信息

cryptos.map(crypto => <li>{crypto.symbol}...</li>)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM