【发布时间】:2019-12-06 02:35:30
【问题描述】:
我有以下代码在 reactJs 中使用 axios 从 API 获取数据
import React, { Component } from 'react';
import axios from 'axios';
export default class App extends Component {
constructor(props) {
super(props);
this.state = {
person: {}
}
}
componentDidMount() {
axios.get('URL', {
method: 'GET',
headers: {
'api-key': 'api-key goes here'
}
})
.then((response) => {
this.successShow(response);
})
.catch((error) => {
this.successShow(error);
});
}
successShow(response) {
this.setState({
person: response.data
});
}
render() {
return (
<div>
<h2>Welcome to React</h2>
<p>{JSON.stringify(this.state.person.data)}</p>
<div>{this.member}</div>
</div>
);
}
}
结果如下图所示
{"Table":[{"id":1001,"first_name":"Adam","last_name":"Quda",.....}]}
我想将数据显示为
First Name Last Name
Adam Quda
. .
. .
我试过this.state.person.data.first_name,{this.state.person.data.Table[].first_name},但有时我会出错
未定义函数名
first_name或Table..
【问题讨论】: