Reactjs React Native null不是对象';this.state.dataSource.map';
实际上,我试图在react native中使用fetch方法,方法如下/prenotazioni返回以下jsonReactjs React Native null不是对象';this.state.dataSource.map';,reactjs,react-native,Reactjs,React Native,实际上,我试图在react native中使用fetch方法,方法如下/prenotazioni返回以下json [{"DESCLI_PTV":"We","ORAIN_PTV":"2019-10-09T10:00:00.000Z"},{"DESCLI_PTV":"igor","ORAIN_PTV":"2019-10-09T11:00:00.000Z"},{"DESCLI_PTV":"Giovanni","ORAIN_PTV":"2019-10-09T12:00:00.000Z"},{"DESCL
[{"DESCLI_PTV":"We","ORAIN_PTV":"2019-10-09T10:00:00.000Z"},{"DESCLI_PTV":"igor","ORAIN_PTV":"2019-10-09T11:00:00.000Z"},{"DESCLI_PTV":"Giovanni","ORAIN_PTV":"2019-10-09T12:00:00.000Z"},{"DESCLI_PTV":"Veronica","ORAIN_PTV":"2019-10-09T14:00:00.000Z"},{"DESCLI_PTV":"We","ORAIN_PTV":"2019-10-09T13:00:00.000Z"},{"DESCLI_PTV":"Giovanni","ORAIN_PTV":"2019-10-09T20:00:00.000Z"},{"DESCLI_PTV":"Igor","ORAIN_PTV":"2019-10-10T11:00:00.000Z"},{"DESCLI_PTV":"Giovanni","ORAIN_PTV":"2019-10-10T13:00:00.000Z"},{"DESCLI_PTV":"We","ORAIN_PTV":"2019-10-10T12:00:00.000Z"},{"DESCLI_PTV":"We","ORAIN_PTV":"2019-10-10T15:00:00.000Z"},{"DESCLI_PTV":"Igor Mytyuk","ORAIN_PTV":"2019-10-11T14:10:00.000Z"}]
但当我试图运行应用程序时,它返回错误
'null不是对象(正在评估'this.state.dataSource.map')
这里是Dashboard.JS
import React from 'react';
import { FlatList, ActivityIndicator, Text, View } from 'react-native';
export default class Dashboard extends React.Component {
constructor(props){
super(props);
this.state ={ isLoading: true}
}
componentDidMount(){
return fetch('192.168.100.160:3000/prenotazioni')
.then((response) => response.json())
.then((responseJson) => {
this.setState({
isLoading: false,
dataSource: responseJson,
}, function(){
});
})
.catch((error) =>{
console.error(error);
});
}
render(){
if(this.state.isLoading){
return(
<View style={{flex: 1, padding: 20}}>
<ActivityIndicator/>
</View>
)
}
return(
<View style={{flex: 1, paddingTop:20}}>
<FlatList
data={this.state.dataSource}
renderItem={({item}) => <Text>{item.DESCLI_PTV}, {item.ORAIN_PTV}</Text>}
keyExtractor={({id}, index) => id}
/>
</View>
);
}
}
从“React”导入React;
从“react native”导入{FlatList,ActivityIndicator,Text,View};
导出默认类Dashboard扩展React.Component{
建造师(道具){
超级(道具);
this.state={isLoading:true}
}
componentDidMount(){
返回获取('192.168.100.160:3000/prenotazioni')
.then((response)=>response.json())
.然后((responseJson)=>{
这是我的国家({
孤岛加载:false,
数据来源:responseJson,
},函数(){
});
})
.catch((错误)=>{
控制台错误(error);
});
}
render(){
if(此.state.isLoading){
返回(
)
}
返回(
{item.DESCLI_PTV},{item.ORAIN_PTV}
keyExtractor={({id},索引)=>id}
/>
);
}
}
在状态下缺少数据源:[]
。所以试试看
this.state ={ isLoading: true, dataSource: [] }
状态中缺少数据源:[]
。所以试试看
this.state ={ isLoading: true, dataSource: [] }