最佳答案
更改此:
const db = firebase.database().ref('users/trainers/'+uid).on('value', function(snapshot){
// here this refers to the firebase callback scope
this.setState({
.........
.........
})
}))
进入此:
const db = firebase.database().ref('users/trainers/'+uid).on('value', (snapshot) =>{
// here this refers to the firebase callback scope
this.setState({
.........
.........
})
}))
使用箭头函数:
An arrow function does not have its own this. The this value of the enclosing lexical scope is used; arrow functions follow the normal variable lookup rules. So while searching for this which is not present in current scope, an arrow function ends up finding the this from its enclosing scope.
关于javascript - 直接从 firebase rtdb 获取数据时出现 TypeError : Cannot read property 'setState' of null,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59943344/