React - Preventing Form Submission

前端 未结 11 868
一个人的身影
一个人的身影 2020-11-27 06:35

I\'ve been experimenting with React. In my experiement, I\'m using the Reactstrap framework.When I click a button, I\'ve noticed that the HTML form submits. Is there a way t

11条回答
  •  攒了一身酷
    2020-11-27 07:36

    2 ways

    First one we pass the event in the argument right into the onClick.

      onTestClick(e) {
        e.preventDefault();
        alert('here');
      }
    

      // Look here we pass the args in the onClick
       
    

    Second one we pass it into argument and we did right in the onClick

      onTestClick() {
        alert('here');
      }
    

      // Here we did right inside the onClick, but this is the best way
       
    

    Hope that can help

提交回复
热议问题