我使用axios.get的回調來設置React組件的狀態。響應的data屬性包含一個用於設置狀態的對象數組。安裝組件和setState後返回undefined的對象屬性
當我將狀態屬性記錄到控制檯時,沒有問題,我可以看到對象數組。但是,如果我嘗試登錄這些對象的一個單獨我得到的錯誤:
不能ResponsiveTable.render
看空的財產「0」下面是我的組件代碼:
class ResponsiveTable extends React.Component {
constructor (props) {
super(props)
this.state = {
returnedQuery: null
};
}
componentDidMount() {
axios.get('/api/latestLeads')
.then((response) => {
this.setState({
returnedQuery: response.data
});
})
.catch(function (error) {
console.log(error);
});
}
render() {
console.log(this.state.returnedQuery[0]);
return (
<div>
<h1>test</h1>
</div>
);
}
}
'render'是'componentDidMount' – azium