【发布时间】:2019-10-29 15:52:11
【问题描述】:
我有这段代码用于显示一个孩子,但是否可以遍历所有孩子并一次显示全部?这是我的 firebase 数据库image link 的结构,任何有关我如何做到这一点的信息都会很棒,我已经阅读过关于像 JSON 一样映射数据但是在通过前端解析代码时我很挣扎一点点。谢谢!
import React, { Component } from 'react';
import styles from './Styles.js';
import {
Text,
View,
Image,
Button,
TouchableOpacity,
Dimensions,
ScrollView,
StatusBar,
} from 'react-native';
import { LinearGradient } from 'expo';
import * as firebase from 'firebase';
export class Dog extends Component {
componentDidMount(){
firebase.database().ref('pets/1').once('value', (snapshot) => {''
const snap = snapshot.val();
this.setState({
name: snap.name,
petlocation: snap.location,
type: snap.type,
reward: snap.reward,
time: snap.time,
phone: snap.phone
})
});
}
constructor(props) {
super(props);
this.state = {
pets: {
name: '',
petlocation: '',
type: '',
reward: '',
time: '',
phone: null,
}
};
}
render() {
return (
<View style={styles.locpetinfo}>
<Image style={styles.locpetimg} source={require('assets/dog.jpg')} />
<Text style={styles.locpetname}>{this.state.name}</Text>
<Text style={styles.locpetpos}>{this.state.petlocation}</Text>
<Text style={styles.locpettype}>{this.state.type}</Text>
<Text style={styles.loctime}>{this.state.time}</Text>
<Text style={styles.locbounty}>{this.state.reward}</Text>
<Text style={styles.locbountysub}>REWARD</Text>
</View>
);
}
}
export default Dog;
【问题讨论】:
标签: javascript firebase react-native firebase-realtime-database expo