Why is my function being called twice in React?

前端 未结 4 1614
被撕碎了的回忆
被撕碎了的回忆 2020-12-15 17:59

I have an idea that this may be because I am doing some styling things to change my radio button, but I am not sure. I am setting an onClick event that is calling my functio

4条回答
  •  -上瘾入骨i
    2020-12-15 18:51

    The problem is html related, rather than React related. By default clicking a label will also trigger the onClick event of the input element it is associated with. In your case the onClick event is attached to both the label and the input. So by clicking on the label, the event is fired twice: once for the label and once for the input associated with it.

    Edit: Attaching the onClick listener to the input is a possible solution to the problem

提交回复
热议问题