即時通訊新的反應,我試圖顯示響應數據,我從一個get request.I一直在尋找多個類似的職位,試圖實現什麼爲他人工作,但對我來說沒有任何工作。在我的代碼下面,我沒有得到任何錯誤(設置狀態似乎也工作)但沒有顯示,我不知道爲什麼。有任何想法嗎?謝謝!React - 通過獲取請求中的響應數據進行映射 - 爲什麼不顯示響應數據?
class Testing extends React.Component {
constructor(props) {
super(props);
this.state = {
name: []
};
this.getData = this.getData.bind(this)
}
componentDidMount() {
this.getData();
}
getData() {
axios.get('https://example.com)
.then(results => {
return results.data;
})
.then(res => {
let arr = res.items;
let test = [];
return arr.map(function(item) {
test.push(item);
})
this.setState({
name: test
});
})
}
render() {
const persons = this.state.name.map((item, i) => {
return
<div>
<h1> {item.name} </h1>
</div>
});
return
<div id = "layout-content" className = "layout-content-wrapper" >
<div className = "panel-list">
All: {persons}
</div>
</div>
}
}
改變渲染:
class Testing extends React.Component {
constructor(props) {
super(props);
this.state = {
name: []
};
this.getData = this.getData.bind(this)
}
componentDidMount() {
axios.get('https://example.com)
.then(results => {
return results.data;
})
.then(res => {
let arr = res.items;
let test = [];
return arr.map(function(item) {
test.push(item);
})
this.setState({
name: test
});
})
}
render() {
<div>
this.state.name ? this.state.name.map((item, i) => {
return (
<div>
<h1>{ item.name }</h1>
</div>
)
}) : null;
</div>
}
}
錯誤的位置:
<div>
this.state.name ? this.state.name.map((item, i) => {
return (
^
<div>
<h1>{ item.name }</h1>
</div>
謝謝。我嘗試瞭解決方案,但仍然沒有顯示任何內容。我認爲問題出在我的渲染功能上? – javascripting
我不認爲你的第一個代碼片段中的rendor函數有任何問題。你確定你的數據來自api嗎? –
是的。當我console.log的數據,它實際上顯示數據,以便工作正常.. – javascripting