Ad

Access Function Within Quotes

I am getting some data (foo) from firebase db. Currently, I can only access this data with my function displayValue(). Although I am lost in how I can place this function within my video tag. Because I need to get the data first from firebase, the functions needs to be between url={..}, but I'm not sure how to do this.

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
...
Ad

Answer

Try this:

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} />
  }
}
Ad
source: stackoverflow.com
Ad