javascript - Undefined 不是对象——React Native
问题描述
嗨,我是本机反应的新手,我正面临这个错误 TypeError:undefined is not an object (evalating this.state.items) 另一个问题是它正在返回数组中的数据我如何将数据显示为字符串
import React, { Component } from "react";
import {StyleSheet,View,ActivityIndicator,FlatList,Text,TouchableOpacity} from "react-native";
export default class Source extends React.Component {
static navigationOptions = ({ navigation }) => {
return {
title: "Source Listing",
headerStyle: {backgroundColor: "#fff"},
headerTitleStyle: {textAlign: "center",flex: 1}
};
};
constructor(props) {
super(props);
this.state = {
loading: false,
items:[]
};
this.fetchRequest=this.fetchRequest.bind.this
}
FlatListItemSeparator = () => {
return (
<View style={{
height: .5,
width:"100%",
backgroundColor:"rgba(0,0,0,0.5)",
}}
/>
);
}
componentDidMount()
{
fetchRequest();
}
renderItem=(data)=>
<TouchableOpacity style={styles.list}>
<Text style={styles.lightText}>{data.item.name}</Text>
<Text style={styles.lightText}>{data.item.email}</Text>
<Text style={styles.lightText}>{data.item.company.name}</Text>
</TouchableOpacity>
render(){
<FlatList
data= {this.state.dataSource}
ItemSeparatorComponent = {this.FlatListItemSeparator}
renderItem= {item=> this.renderItem(item)}
keyExtractor= {item=>item.id.toString()}
/>
fetchRequest()
{
const parseString = require('react-native-xml2js').parseString;
fetch('http://192.168.200.133/apptak_service/apptak.asmx/Get_Item_Master')
.then(response => response.text())
.then((response) => {
parseString(response, function (err, result) {
console.log(response)
});
}).catch((err) => {
console.log('fetch', err)
this.fetchdata();
})
if(this.state.loading){
return(
<View style={styles.loader}>
<ActivityIndicator size="large" color="#0c9"/>
</View>
)}}
return(
<View style={styles.container}>
</View>
)}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#fff"
},
loader:{
flex: 1,
justifyContent: "center",
alignItems: "center",
backgroundColor: "#fff"
},
list:{
paddingVertical: 4,
margin: 5,
backgroundColor: "#fff"
}
});
基本上我想做的是从网络服务获取数据并在屏幕上显示应用程序
解决方案
推荐阅读
- angular - 如何删除 RXJS 中的冗余请求?
- regex - Regexp Ruby 如何与“!=”完全匹配,而不是仅与 =
- javascript - 无法获取编辑页面。使用 mysql 和 javascript 时出错
- css - VueJS背景图像网址找不到路径
- firebase - 如何修复错误:未处理的承诺拒绝:this._next 不是函数;区:
; 任务:ServiceWorkerContainer.addEventListener:message - php - 使用 Paypal - 通过 PHP curl 预先连接路径
- bash - 用于检查可用驱动器空间的 Bash 命令(是“检查十进制数的值”)
- salesforce - 根据自定义字段为客户创建一个 Apex 触发器,以匹配发货地址邮政编码和帐单地址邮政编码
- python - 如何访问字典内的数组内的项目索引?
- sql - 在子查询中只返回一行