我需要幫助確定爲什麼地圖函數不能按預期工作。我在ComponentDidMount中放置了一個AJAX請求,我能夠在控制檯日誌中看到數據,但是我無法訪問它。我假設數據沒有被傳入setState()。React渲染:object.map()不是函數
import React from 'react';
import $ from 'jquery';
export default class Bitcoin extends React.Component{
constructor(props){
super(props);
this.state = {
prices:[]
}
}
componentDidMount(){
$.ajax({
type: 'GET',
url: 'https://index.bitcoin.com/api/v0/lookup?time=2017-01-01T06:00:00Z',
dataType: 'json',
success: function(data) {
console.log(data)
this.setState({prices: data});
}.bind(this),
error: function(xhr, status, err) {
console.error(this.props.url, status, err.toString());
}.bind(this)
});
}
render(){
const { prices } = this.state;
return(
<div>
<p>
{
prices.map(function(price){
return <p key={price.code}>{price.description}{price.rate}</p>
})
}
</p>
</div>
)
}
}
控制檯輸出
對象沒有指定映射函數。一個數組可以。你的數據響應是一個對象,所以它輸出我沒有地圖功能。 –