我得到一个TypeError:这是未定义的,当我在componentDidMount方法中使用它时,即使我相信我绑定了该方法。我是否错误地绑定了方法或是否存在其他问题?我试图使用两个箭头函数并将其绑定在构造函数中,但我得到相同的错误。
constructor(...args) {
super(...args);
// this.handleSubmit = this.handleSubmit.bind(this);
this.state = {
email: null,
address: null,
address2: null,
city: null,
state: null,
zip: null,
subscribe: null,
currentUser: null,
displayName: null,
uid: null,
}
this.handleChange = this.handleChange.bind(this);
this.componentDidMount = this.componentDidMount.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
componentDidMount = () => {
firebaseRef.auth().onAuthStateChanged(function(user) {
if (user) {
[...]
})
this.setState({
uid: user.uid,
displayName: user.displayName});
db.collection("testCollection").doc("handleSubmitTest8").set({
// uid:this.state.uid,
test: "testy",
}).then(function() {
console.log("Document written in handleSumit in NUM");
})
.catch(function(error) {
console.error("Error adding document: ", error);
});
} else {
console.log("no user signed in");
}
});
}
handleSubmit = (event) => {
[...]
}
handleChange(event) {
[...]
}
render() {
return (...)}}```
绝地无双
相关分类