从“ react-native”导入{AppRegistry,StyleSheet,flatList,文本,视图,警报,activityIndicator,平台};
类UserList扩展了组件{
构造函数(道具) {
super(props);
this.state = {
isLoading: true,
} }
componentDidmount(){ fetch('http://000.000.0.000:0000/api/useraccount/user',{ 方法:“ GET”, 标头:{ 'accept':'application / json', 'Content-Type':'application / json', 'auth-token':'我的令牌' }
})
.then((response) => response.json())
.then((responseJson) => {
this.setState({
isLoading: false,// dataSource: JSON.parse(responseJson),dataSource: JSON.stringify(responseJson) ===>i think this is were problem
});
if (responseJson) {
Alert.alert("Id is" + JSON.stringify(responseJson)) ==>this give me an alert
console.log(responseJson)
}
else if (responseJson.error) {
Alert.alert(responseJson.error);
}
})
.catch((error) => {
console.error(error);
})
}
flatListItemSeparator =()=> { 返回( ); }
render(){
if (this.state.isLoading) {
return (
<View style={{flex: 1,paddingTop: 20}}>
<activityIndicator />
</View>
);
}
return (
<flatList
data={ this.state.dataSource }
ItemSeparatorComponent = {this.flatListItemSeparator}
renderItem={({item}) => <Text > {item.name} </Text>}
keyExtractor={(item,index) => index}
/>
);
} }
导出默认用户列表;
const styles = StyleSheet.create({
MainContainer:{
justifyContent:“中心”, flex:1, 保证金:10, paddingTop:(Platform.OS ==='ios')? 20:0,
},
flatListItemStyle:{ 填充:10, fontSize:18, 高度:44 }
});