【发布时间】:2019-10-16 15:37:06
【问题描述】:
我一直在研究 react js,想 setState 获取来自 firebase 的回复。
因此,当我尝试为状态设置值时,它会返回错误,
无法读取未定义的属性“setState”
我的代码。
class Login extends Component {
constructor(props) {
super(props)
this.state = {email:'',
uid:'',
password :'',
}
this.handleChange = this.handleChange.bind(this)
this.handleSubmit = this.handleSubmit.bind(this)
this.signInWithGoogle = this.signInWithGoogle.bind(this)
}
handleChange(event) {
this.setState({
[event.target.id] : event.target.value
})
console.log(this.state.email)
console.log(this.state.password)
}
handleSubmit(e) {
e.preventDefault()
firebase.auth().signInWithEmailAndPassword(this.state.email, this.state.password).then(function(result) {
console.log(result.user.uid)
this.setState({ [uid]: [result.user.uid] });
}).catch(function(error) {
console.log(error.code)
console.log(error.message)
alert(error.message)
})
}
我确实有一个表格可以提供输入。 有人可以帮我解决这个问题吗?
【问题讨论】:
-
handleChange 或 handleSubmit 中出现错误?
-
你需要将'signInWithEmailAndPassword(() => {})'的回调作为箭头函数。那么只有父/类“this”上下文可用。
标签: javascript reactjs firebase