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

后端 未结 2 1385
时光取名叫无心
时光取名叫无心 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:19

    I am pretty sure that since you are capturing the click of a button from a form, without the preventDefault() the form is posting. Since there are no inputs in the form there are no query parameters. Since the form doesn't specify the POST method it is doing a get request back to itself which is adding an empty query string. With the preventDefault() you are stopping the form submit action.

提交回复
热议问题