Reputation: 150
I'm using Firebase Realtime Storage. I'm trying to call function from inside the firebase ref.on() function.
My code:
export default class HomeScreen extends React.Component {
constructor(props) {
super(props);
ref.on("value", function (snapshot) {
dosomething(snapshot.val()) //<-- not recognized
}, function (errorObject) {
console.log("The read failed: " + errorObject.code);
});
}
dosomething(val){
//...
}
//....
});
I'm trying to make the dosomething()
function to get called from the ref.on()
function but React don't recognize the dosomething()
function
Why is it happened recognize and How do I fix it?
Thanks for your help :)
Upvotes: 0
Views: 177
Reputation: 6254
you'll need to change function (snapshot) {
to (snapshot) => {
now the this
inside that callback is the this
value of the enclosing lexical context - i.e the constructor
and then you can use this.dosomething
- because dosomething
is a property of this
export default class HomeScreen extends React.Component {
constructor(props) {
super(props);
ref.on("value", (snapshot) => {
this.dosomething(snapshot.val())
}, function (errorObject) {
console.log("The read failed: " + errorObject.code);
});
}
dosomething(val){
//...
}
//....
});
Upvotes: 2