React应用程序:所有三个按钮都被自己单击,如何防止这种情况发生(新来的反应)

编程入门 行业动态 更新时间:2024-09-27 19:17:45

React应用程序:所有三个按钮都被自己单击,如何防止<a href=https://www.elefans.com/category/jswz/34/1771290.html style=这种情况发生(新来的反应)"/>

React应用程序:所有三个按钮都被自己单击,如何防止这种情况发生(新来的反应)

这是我调用API的函数:底部是两个自动单击的按钮。任何帮助都是非常有帮助的,因为我是一个初学者,对此做出反应和js

postData = m => {
    const config = {
      headers: {
        "Content-Type": "application/json",
        apiRegistrationId: "46",
      },
    };
    const data = {
      referenceNumber: "string",
      sourceRequestId: m
    };

    axios
      .post(
        "",
        data,
        config
      )
      .then((data) => {
        console.log(data.GatewayUrl);
        window.location.href = data.GatewayUrl;
      })
      .catch((err) => {
        console.log(err);
      });
  }

  render() {
    return (
      <div>
        <h1>Donate to the local food shelter</h1>
        <button onClick={this.postData(5)}>Donate $5</button>
        <button onClick={this.postData(10)}>Donate $10</button> 
      </div>
    );
  }
}

export default Home;
回答如下:

只需:

  <button onClick={() => this.postData(5)}>Donate $5</button>
  <button onClick={() => this.postData(10)}>Donate $10</button> 

更多推荐

React应用程序:所有三个按钮都被自己单击,如何防止这种情况发生(新来的反应)

本文发布于:2024-05-07 11:29:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1756039.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:这种情况   新来   单击   应用程序   按钮

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!