【问题标题】:Access function within quotes引号内的访问函数
【发布时间】:2019-04-02 11:33:04
【问题描述】:

我正在从 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】:

    试试这个:

    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} />
      }
    }
    

    【讨论】:

    • 快乐编码:)
    猜你喜欢
    • 2015-11-07
    • 2011-09-18
    • 2017-11-11
    • 1970-01-01
    • 2014-03-07
    • 2023-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多