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 :)
you'll need to change function (snapshot) {
to (snapshot) => {
now the this
inside that callback is the this
value of the enclosing lexical context - ie 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){
//...
}
//....
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.