【问题标题】:Need help to fetch API需要帮助来获取 API
【发布时间】:2020-08-24 16:06:30
【问题描述】:

我正在尝试获取 API,但数据未呈现在我的页面上。我想我做错了什么。可以帮帮我吗,我添加了一张要获取的数据的照片,我的代码也是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>
        )
    }
}

【问题讨论】:

    标签: javascript reactjs api fetch


    【解决方案1】:

    从 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})
     }
    

    您的地图中还有显示信息的方法

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-03
      • 1970-01-01
      • 1970-01-01
      • 2023-04-07
      • 2019-12-31
      • 2012-05-10
      • 2020-02-09
      相关资源
      最近更新 更多