引号内的访问功能

我正在从Firebase数据库获取一些数据(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

...


潇潇雨雨
浏览 137回答 1
1回答

撒科打诨

试试这个:class YourComponent {&nbsp; &nbsp; state = {&nbsp; &nbsp; &nbsp; &nbsp; foo: null&nbsp; &nbsp; };&nbsp; componentDidMount() {&nbsp; &nbsp; let foo;&nbsp; &nbsp; firebase&nbsp; &nbsp; &nbsp; .database()&nbsp; &nbsp; &nbsp; .ref('/topics/lec1')&nbsp; &nbsp; &nbsp; .once('value')&nbsp; &nbsp; &nbsp; .then((snapshot) => {&nbsp; &nbsp; &nbsp; &nbsp; foo = snapshot.val();&nbsp; &nbsp; &nbsp; &nbsp; this.setState({ foo });&nbsp; &nbsp; &nbsp; });&nbsp; }&nbsp; render() {&nbsp; &nbsp; &nbsp; const { foo } = this.state;&nbsp; &nbsp; &nbsp; if (!foo) return null;&nbsp; &nbsp; &nbsp; return <Video url={foo} />&nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript