我在componentDidMount中调用Firebase之后尝试设置setState:
class Profile extends Component {
constructor(props) {
super(props)
this.state = {
nick: this.props.match.params.user,
nickid: this.props.match.params.id,
nickuid: null
}
}
componentDidMount() {
firebase.database().ref('/users/').orderByChild('user').equalTo(this.props.match.params.user).once('value').then(function(snapshot) {
var uid = Object.keys(snapshot.val())[0];
this.setState({ nickuid: uid }); // error (uid is not null)
console.log(uid);
});
}
我有Unhandled Rejection (TypeError): Cannot read property 'setState' of undefined
个想法,为什么?
答案 0 :(得分:3)
在此处使用箭头功能。
通过O(log(n)
时,您失去了上下文
试试:
function
答案 1 :(得分:1)
使用箭头功能。
for list in a:
for sublist in list:
for item in sublist:
if item == "0" or item == "1":
item == int(item)