【发布时间】:2018-01-09 11:23:27
【问题描述】:
我是 React Native 的新手,我尝试使用 React Native 中的“fetch”方法获取学生记录并保存响应(我的响应看起来像)
{"student":[{"student_id":"1","student_name":"JHON","student_class_id":"22"},
{"student_id":"2","student_name":"BONY","student_class_id":"22"}],"error":false})
到dataSource如下
.then((responseJson) => {
let ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
this.setState({
dataSource: ds.cloneWithRows(responseJson.student)
}, function() {
// do something with new state
});
})
然后像这样渲染它
getClassId(){
// here I want to access rowData.student_class_id
}
render() {
return(
....
....
<View style={styles.card}>
<ListView
dataSource={this.state.dataSource}
renderRow={
(rowData) =>
<View>
<Text style={styles.text}>{rowData.student_id}</Text>
<Text style={styles.text}>{rowData.student_name}</Text>
</View>
}
/>
</View>
);
}
从响应中我们可以看到“student_class_id”字段。我不想在文本视图中显示它。但我想在这个渲染方法之外访问这个值。那是同一类的其他方法中的一些地方。
我没有得到任何可行的解决方案。请指导我实现它。任何建议表示赞赏。
【问题讨论】:
标签: react-native fetch