首页 > 解决方案 > 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"
   }
});

基本上我想做的是从网络服务获取数据并在屏幕上显示应用程序

标签: javascriptreact-native

解决方案


推荐阅读