onClick 不触发点击处理程序

轰炸机

我的点击处理程序没有触发:

submitForm(UserDetails) {
    axios
        .post('http://localhost:3001/api/users', UserDetails)
        .then(function(response) {
            console.log(response);
        })
        .catch(function(error) {
            console.log(error);
        });
} 

在我的按钮上:

<button
            className="btn btn-primary"
            onClick={this.submitForm(this.props.UserDetails)}>
            Upload
        </button>

我已将其绑定到此,在我的构造函数中:

constructor() {
    super();
    this.submitForm = this.submitForm.bind(this);
}

有任何想法吗?

卡里姆

在 onClick 挂钩中,您正在调用该函数而不是传递该函数的引用

<button
  className="btn btn-primary"
  onClick={this.submitForm(this.props.UserDetails)}>
  Upload
</button>

从构造函数中删除绑定

this.submitForm = this.submitForm.bind(this);

并将适当的函数传递给 onClick

<button
  className="btn btn-primary"
  onClick={(e) => this.submitForm(this.props.UserDetails)}>
  Upload
</button>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章