我不知道爲什麼我的axios promise的結果不會顯示在呈現函數中。順便說一句,我正在使用create-react-app工具。Ajax請求不會在呈現函數中顯示
_getPrice() {
const url = 'https://api.coinbase.com/v2/prices/BTC-USD/spot';
axios.get(url)
.then(function (response) {
//console.log(response.data.data.amount);
let prices = response.data.data.amount;
return prices;
})
}
render() {
return(<div><h3> {this._getPrice()} </h3></div>);
}
Wel ......你可以在'componentDidMount'和'_getPrice'函數中調用'this._getPrice()',在獲得數據之後,你可以使用'setState'將它存儲在狀態中,並且可以顯示數據狀態如果數據不存在,您可以顯示加載消息..,這有幫助嗎? –