【发布时间】:2020-09-01 19:43:19
【问题描述】:
我正在尝试使用 Axios 从 API 呈现名称。即使我可以在控制台中看到所有数据,但由于显示错误而无法呈现:
users.map 不是函数
下面我将分享我的文件代码。我很新,这可能是我无法弄清楚的原因。
import React from 'react';
import './App.css';
import 'bootstrap/dist/css/bootstrap.min.css'
import { Container, Table} from "react-bootstrap";
import axios from 'axios';
class App extends React.Component {
state = {
users: [],
};
componentDidMount () {
axios.get('https://5w05g4ddb1.execute-api.ap-south-1.amazonaws.com/dev/profile/listAll')
.then(response => {
const users = response.data;
this.setState({ users });
console.log(this.state.users);
})
}
render() {
const { users } = this.state;
return (
<div className="App">
<Container fluid>
<Table striped bordered hover size="sm">
<thead>
<tr>
<th><div id="">Image</div></th>
<th>Name</th>
<th>Gender</th>
<th>Age</th>
<th>Date</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<tr>
{ users.map(user => { return <td key={user.id}>{ user.name }</td> }) }
</tr>
</tbody>
</Table>
</Container>
</div>
)
}
}
export default App;
【问题讨论】:
-
因为您请求的响应是一个对象,而不是一个数组。看起来您需要将状态设置为响应的
list属性。
标签: javascript reactjs react-native react-redux axios