Why does a react onClick event append a question mark to my url?

后端 未结 2 1416
时光取名叫无心
时光取名叫无心 2021-02-08 00:03

For some reason my onClick handlers are adding an empty query param to my url when I click them. I was able to fix the issue by adding event.preventDefault to my event handlers

2条回答
  •  自闭症患者
    2021-02-08 00:22

    The default type of a button tag is submit which means clicking the button will submit your form (appending the ? to your url).

    To fix your example, you can add type="button" to your buttons &/or replace your

    with a
    / (since your code doesn't really seem to need the form element).

    https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button

    Possible values are:

    • submit: The button submits the form data to the server. This is the default if the attribute is not specified, or if the attribute is
      dynamically changed to an empty or invalid value.
    • reset: The button resets all the controls to their initial values.
    • button: The button has no default behavior. It can have client-side scripts associated with the element's events, which are triggered when the events occur.

提交回复
热议问题