OnDoubleClick in react

88 Views Asked by At

I can't get it to respond onDoubleClick. neither with console.log. I do not understand why, since the code looks good. I hope your help thank you very much, bye.

}

deleteUser = async(id) => {

    await axios.delete('http://localhost:4000/users/' + id)
    this.getUsers();

}

                           this.state.users.map(user => (
                            <li 
                                className="list-group-item list-group-item-action" 
                                key={user._id}
                                onDoubleClick={() => this.deleteUser(user._id)}
                                >
                                {user.username}
                            </li>)
                        )


                           
2

There are 2 best solutions below

1
On

Add .bind(this) hope you achieve what you want.

 <li className="list-group-item list-group-item-action" 
      key={user._id}
      onDoubleClick={ this.deleteUser(user._id).bind(this)}>
      {user.username}
 </li>

0
On

Here you need to bind your method before using in template

this.doublemethod = this.doublemethod.bind(this); // in your constructor

Note: https://stackblitz.com/edit/react-juq9t6