引号内的访问功能

问题描述 投票:0回答:1

我从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
...
javascript firebase react-native firebase-realtime-database
1个回答
1
投票

试试这个:

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} />
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.