import React, { Component } from 'react';
import axios from 'axios';
import './App.css';
class App extends Component {
state = {user:[]}
componentDidMount(){
axios.get('http://a99d12de.ngrok.io/api/Docs')
.then(res => {
const user = res.data;
this.setState({user});
})
.catch((error) =>{
alert(error);
})
}
render() {
return (
<div>
<h1> names</h1>
<ul>{this.state.user.map(person => <li>{person.data.fname}</li>)}</ul>
</div>
)
}
}
export default App;
请纠正我的错误..我只是想从服务器/ url中纠正一些数据。我不知道为什么没有定义地图功能。
答案 0 :(得分:0)
render(){
let array = this.state.user || []
return(
<div>
<h1> names</h1>
<ul>
{array.map((item,index) => ( <li key={index}> {item.fname}</li> ))}
</ul>
</div>
答案 1 :(得分:0)
您的回复数据可能是.vue
,您必须使用JSON String
JSON.parse(user)
答案 2 :(得分:0)
Axios将响应包装在具有data
属性的对象中。所以我想你必须做这样的事情:
axios.get('http://a99d12de.ngrok.io/api/Docs')
.then(res => {
// axios wrapped the data in a response object with a data property
const data = res.data;
// Your API also wrapped the data in a response object with a data property :)
const user = data.data;
this.setState({user});
})
.catch((error) =>{
alert(error);
})
}
请参阅axios docs here。