我从firebase db获取了一些数据(foo)。目前,我只能使用我的函数displayValue()来访问这些数据。虽然我对如何在视频标签中放置此功能感到迷茫。因为我需要首先从firebase获取数据,所以函数需要在url = {..}之间,但我不知道如何做到这一点。
var foo;
firebase
.database()
.ref(`/topics/lec1`)
.once("value")
.then(function(snapshot) {
foo = snapshot.val();
displayValue(); // after getting value display in fucntion
});
function displayValue() {
//foo
//value foo from firebase
foo;
}
return(
<Video url={displayValue(..)} // foo value needs to show here after getting value
...
试试这个:
class YourComponent {
state = {
foo: null
};
componentDidMount() {
let foo;
firebase
.database()
.ref('/topics/lec1')
.once('value')
.then((snapshot) => {
foo = snapshot.val();
this.setState({ foo });
});
}
render() {
const { foo } = this.state;
if (!foo) return null;
return <Video url={foo} />
}
}