【发布时间】:2017-12-20 05:00:33
【问题描述】:
我正在做一个反应原生的单页应用程序。我想在列表视图中显示获取响应。为了访问响应数组,我正在使用 map 方法,但出现上述错误。来自我的服务器的响应如下所示
{
"responseHeader": {
"type": "314",
"status": "200",
"message": "Successfully found the profile"
},
"neighbourProfile": [{
"no_of_mutual_friends": "0",
"username": "Amith Issac",
},
{
"no_of_mutual_friends": "0",
"username": "Liz",
}]
}
这里如何使用map方法?而我的app.js如下
app.js
import React, { Component } from 'react';
import { ScrollView } from 'react-native';
import Neighbour from './src/components/Neighbour';
class App extends Component {
state = { neighbours: [] };
componentWillMount(){
const myArray =
{"requestHeader":
{
"personal_id": "122334",
"type":"314"
}
}
fetch(" https://xxxxx",{
method: 'POST',
headers:{
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(myArray)
})
.then((response) => response.json())
.then((responseData) =>this.setState({neighbours: responseData}));
}
renderNeighbour(){
return this.state.neighbours.map(neighbours =>
<Neighbour key ={neighbours.username} neighbours={neighbours}/>
);
}
render() {
return (
<ScrollView>
{this.renderNeighbour()}
</ScrollView>
);
}
}
export default App;
我做错了什么?
【问题讨论】:
-
你在使用列表视图组件吗?你想从 neighbourProfile 映射吗?
-
我想列出响应,因为我正在使用这个映射方法
标签: javascript arrays json react-native