如何解决event.preventdefault创建按钮时的解析错误
我是新手反应和努力创建登录按钮的新手。我会收到以下错误: ./src/form.js 第22行:解析错误:意外令牌
20 | }; 21 |
22 | event.preventdefault; | ^ 23 | 24 |使成为() { 25 |返回(
我的代码:
import React, { Component } from 'react';
import classes from './Form.module.css';
class Form extends Component {
constructor(props) {
super(props);
this.state = {
username: ''
};
}
handleUsernameChange = (event) => {
this.setState({
username: event.target.value
});
};
handleLoginChange = (event) => {
alert('${this.state.username} ');
};
event.preventDefault;
render() {
return (
<form className={classes.Form} on Login = {this.handleLogin}>
<label>Username</label>
<input
type="text"
value={this.state.username}
onChange={this.handleUsernameChange}
></input>
<button type="login">Login</button>
</form>
);
}
}
export default Form;
预先感谢!
I am new to React and struggling to create a login button. I get the following error:
./src/Form.js
Line 22: Parsing error: Unexpected token
20 | };
21 |
22 | event.preventDefault;
| ^
23 |
24 | render() {
25 | return (
My code:
import React, { Component } from 'react';
import classes from './Form.module.css';
class Form extends Component {
constructor(props) {
super(props);
this.state = {
username: ''
};
}
handleUsernameChange = (event) => {
this.setState({
username: event.target.value
});
};
handleLoginChange = (event) => {
alert('${this.state.username} ');
};
event.preventDefault;
render() {
return (
<form className={classes.Form} on Login = {this.handleLogin}>
<label>Username</label>
<input
type="text"
value={this.state.username}
onChange={this.handleUsernameChange}
></input>
<button type="login">Login</button>
</form>
);
}
}
export default Form;
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,
destressdefault
是一个函数,因此您应该在最后添加()
。然后,如果您可以注意到,则在HandleLogInchange功能之外。尝试以下操作:
First of all,
preventDefault
is a function, so you should add()
at the end. Then if you can notice, is outside the handleLoginChange function.Try this:
将您的event.preventDefault()放入要使用的函数中(请注意,您需要通过括号调用函数)。
还要注意使用Backticks `在使用String内部的JavaScript元素时,如果您选择使用基于字符串的实现。
一旦下课,一定会考虑将来使用钩子和功能组件(少数例外)。
Place your event.preventDefault() inside the function you want to use it for (notice that you need to call the function through parenthesis).
Also note to use backticks ` when using javascript elements inside of a string should you choose to use a string-based implementation.
Once you get classes down, definitely look into using hooks and functional components in the future (with a few exceptions).
因此,
destextdefault()
是一个函数,您已将其放在代码中的每个函数之外。然后
登录
是不正确的,使用onsubmit
并连接一个函数以处理提交是正确的。最后,您必须绑定事件处理程序以纠正上下文(这)。
做您想做的正确的方法是:
如果解决方案有效,请作为正确的答案。
有用的资源:
https://it.reaectjs.org/docs/handling-events.hhandling-events.html
So,
preventDefault()
is a function and you had placed it outside of every function in your code.Then
On Login
is not correct, it would be correct to useonSubmit
and connect a function to handle the submit.In the end, you have to bind your event handlers to correct context (this).
The correct way to do what you want is this:
If the solution works, tick as the correct answer.
useful resources:
https://it.reactjs.org/docs/handling-events.html