我正在使用材料ui设计UI框架
function handleSubmit(){
alert("success");
}
function SignIn(props) {
const { classes } = props;
handleSubmit();
return (<form className={classes.form} onSubmit={this.handleSubmit()}>
<FormControl margin="normal" required fullWidth>
<InputLabel htmlFor="email">Email Address</InputLabel>
<Input id="email" name="email" autoComplete="email" autoFocus />
</FormControl>
</form>
错误堆栈
60 | <Typography component="h1" variant="h5">
61 | Sign in
62 | </Typography>
> 63 | <form className={classes.form} onSubmit={this.handleSubmit()}>
| ^ 64 | <FormControl margin="normal" required fullWidth>
65 | <InputLabel htmlFor="email">Email Address</InputLabel>
66 | <Input id="email" name="email" autoComplete="email" autoFocus />
有人让我知道哪里出了问题吗? `
答案 0 :(得分:0)
答案 1 :(得分:0)
您在这里遇到一些问题:
handleSubmit()
。那只能由您的表格调用onSubmit
中调用处理函数。使用onSubmit={this.handleSubmit}
代替onSubmit={this.handleSubmit()}
。this
对象,因此也没有this.props
。如果使用处理函数,则应使用一个类。请参见下面的示例。
class SignIn extends Component {
constructor(props) {
super(props);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleSubmit = function(){
alert("success");
}
render() {
const { classes } = this.props;
return (<form className={classes.form} onSubmit={this.handleSubmit}>
<FormControl margin="normal" required fullWidth>
<InputLabel htmlFor="email">Email Address</InputLabel>
<Input id="email" name="email" autoComplete="email" autoFocus />
</FormControl>
</form>);
}
}