[英]What is the error in this code of react? When i call the function in part 1 it doesnt work but part 2 works
This is my code.这是我的代码。 when i use function in part 1 it shows nothing on console.
当我在第 1 部分中使用 function 时,它在控制台上没有显示任何内容。 but when i use function in part 2 it shows 'Hello'.
但是当我在第 2 部分中使用 function 时,它显示“你好”。 but i cant see any differences.
但我看不出有什么不同。 What am i doing wrong in part 1?
我在第 1 部分做错了什么?
handleChange = (e) => {
this.setState({
[e.target.id]:e.target.value
})
}
// part 1
handleSubmit = (e) => {
e.prevendDefault()
console.log(this.state)
console.log("hello")
}
// part 2
handleSubmit = (e) => {
e.preventDefault();
console.log(this.state)
console.log('hello')
}
render() {
return (
<div className="form-group container w-50">
<form onSubmit={this.handleSubmit}>
<label htmlFor="">Email</label>
<input type="email" id="email"className="form-control" onChange={this.handleChange} />
<label htmlFor="">Password</label>
<input type="password" id="password" className="form-control" onChange={this.handleChange}/>
<button type="submit" className="btn btn-primary" >Submit</button>
</form>
</div>
);
}
There is a spelling mistake有一个拼写错误
handleSubmit = (e) => {
e.prevendDefault()
console.log(this.state)
console.log("hello")
}
it's preventDefault()
but you had prevendDefault
.它是
preventDefault()
但你有prevendDefault
。
I would suggest using an IDE or editor with IntelliSense
or autocomplete feature to prevent these kinds of mistakes in the future.我建议使用 IDE 或具有
IntelliSense
或自动完成功能的编辑器,以防止将来出现此类错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.