【发布时间】:2018-02-08 01:07:03
【问题描述】:
我是 React 新手,我必须从 API 获取数据并将其显示在表格中。我使用 React Table 来显示表中的数据。如何实现上述内容?目前我在 Google chrome 开发控制台中没有看到来自服务器的任何响应。 React Table 实现使用本地数据工作,但是从 API 填充表不起作用。 我的代码如下:
class TableExp extends React.Component {
constructor() {
super();
this.state = {
tableData: {
resourceID: '',
resourceType: '',
tenantName: '',
dealerID: '',
status: '',
logFilePath: '',
supportPerson: '',
lastUpdatedTime: '',
},
};
}
componentDidMount() {
axios.get(`https://myAPI.restdb.io/rest/mock-data`, {
headers: {'x-apikey': 'apiKEY'}
})
.then(response => {
this.setState({ tableData: response.data.tableData });
//console.log(tableData);
});}
render() {
const { tableData } = this.state;
return (
<div>
<ReactTable
data={tableData}
columns={[
{
Header: 'Details',
columns: [
{
Header: 'Tenant Name',
accessor: '{this.state.tableData.tenantName}',
},
{
Header: 'Support Engineer',
id: '{this.state.tableData.supportEngineer}',
accessor: d => d.supportPerson,
},
],
},
{
Header: 'Info',
columns: [
{
Header: 'Dealer ID',
accessor:'{this.state.tableData.dealerID}',
},
{
Header: 'Status',
accessor:'{this.state.tableData.status}',
},
],
},
{
Header: 'Logs',
columns: [
{
Header: 'File Path',
accessor:'{this.state.tableData.filePath}',
},
],
},
]}
defaultPageSize={10}
className="-striped -highlight"
/>
</div>
);
}
}
export default TableExp;
【问题讨论】:
-
当你
console.log(response)时它会记录什么? -
>app.js:139699 未捕获的错误:模块构建失败:SyntaxError:相邻的 JSX 元素必须包含在封闭标记中 (100:8) >/src/components/dashboard/tableSupport/tableExp. js Module build failed: SyntaxError: Adjacent JSX elements must be Wrap in an enclosure tag (100:8)
-
@SeaWarrior404,
console.log(response),而不是console.log(tableData); -
是的,我编辑了 console.log(tableData) 并用 console.log(response) 更新了它,这是响应 app.js:139699 Uncaught Error: Module build failed: SyntaxError: Adjacent JSX元素必须包含在封闭标记中 (100:8) >/src/components/dashboard/tableSupport/tableExp.js 模块构建失败:SyntaxError: Adjacent JSX 元素必须包含在封闭标记中 (100:8)
-
tableExp.js 文件的第 100 行是什么?您发布的代码没有 100 行。
标签: javascript reactjs axios