javascript - 访问引号内的函数

标签 javascript firebase react-native firebase-realtime-database

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

最佳答案

试试这个:

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

关于javascript - 访问引号内的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55473774/

相关文章:

javascript - 如何从 Firebase 访问项目对象数组

react-native - Realm 数据更改时如何更新 React Native ListView

javascript - strip 错误 : 'Token is Not Supported' when trying to sign up to subscription

java - 如何在 Firestore 中插入对象数组?

android - 如何避免 Firebase 数据库中的重复数据

javascript - 使用 useState 时如何减少 React 中的重复?

javascript - 如何在 React Native 中创建组件映射

javascript - Ajax 无法从 php 加载数据到 div

javascript - 为什么 jQuery 工具工具提示不显示为 jQuery 扩展?

javascript - 在 Mongoose 的字段中定义一个函数作为默认值