button onclick function firing twice

后端 未结 7 1575
天涯浪人
天涯浪人 2020-12-08 00:20

I have a button that calls a javascript function using an event handler. For some reason, the event handler is being called twice.

Here is my button (I am using a ph

7条回答
  •  Happy的楠姐
    2020-12-08 01:01

    For me, I found out my events were not bound to my mapped component. The relevant answer is at Why is my onClick being called on render? - React.js but to provide some insights I've copied some of the answer below (Hope this helps!):

    1. using .bind

    activatePlaylist.bind(this, playlist.playlist_id)
    

    2. using arrow function

    onClick={ () => this.activatePlaylist(playlist.playlist_id) }
    

    3. or return function from activatePlaylist

    activatePlaylist(playlistId) {
      return function () {
         // you code 
      }
    }
    

提交回复
热议问题