我有以下代码,但什么也没显示
state = {
items: []
}
componentWillMount() {
const { uid } = firebase.auth().currentUser.uid;
var usersRef = firebase.database().ref('/users/'+ uid);
usersRef.once('value').then(snapshot => {
items= snapshot.val()
});
}
render() {
const { items } = this.state;
return (
<View>
<Text style={styles.name}>{items.nombre}</Text>
</View>
)}
答案 0 :(得分:0)
您似乎正在尝试将snapshot.val()
设置为this.state.items
,但没有在usersRef调用中引用this.state.items
。
尝试在您的componentWillMount
-
componentWillMount() {
const { uid } = firebase.auth().currentUser.uid;
let usersRef = firebase.database().ref('/users/'+ uid);
usersRef.once('value').then(snapshot => {
this.setState({ items: snapshot.val() });
});
}