Confirm Window in React
Asked Answered
P

4

22

I have this following code :

renderPosts() {
return _.map(this.state.catalogue, (catalogue, key) => {
  return (
    <div className="item col-md-3" key={key} id={key}>
        <img src={this.state.catalogue[key].avatarURL} height={150} with={150}/>
        <h3>{catalogue.marque}</h3>
        <h4>{catalogue.numero}</h4>
        <h4>{catalogue.reference}</h4>
        <p>{catalogue.cote}</p>
        <div className="text-center">
        <button className="btn btn-danger" onClick={() => {if(window.confirm('Delete the item?')){this.removeToCollection.bind(this, key)};}}>Supprimer</button>
        </div>

    </div>
  )
 })
}

And I have this function too:

removeToCollection(key, e) {

  const item = key;
  firebase.database().ref(`catalogue/${item}`).remove();
 }

When I use the function without a confirm window in my "onclick" button, the code work great. But when I want use a confirm window, the confirm window show when I click on my button, but my item is not delete.

Any idea ?

Thank for your help !

Plenteous answered 27/8, 2018 at 7:51 Comment(0)
X
33

Basically you're binding the function instead of calling it... you should bind beforehand, preferably in the constructor... then call it. Try this:

renderPosts() {
  this.removeToCollection = this.removeToCollection.bind(this);
  return _.map(this.state.catalogue, (catalogue, key) => {
    return (
      <div className="item col-md-3" key={key} id={key}>
          <img src={this.state.catalogue[key].avatarURL} height={150} with={150}/>
          <h3>{catalogue.marque}</h3>
          <h4>{catalogue.numero}</h4>
          <h4>{catalogue.reference}</h4>
          <p>{catalogue.cote}</p>
          <div className="text-center">
          <button className="btn btn-danger" onClick={() => {if(window.confirm('Delete the item?')){this.removeToCollection(key, e)};}}>Supprimer</button>
          </div>

      </div>
    )
  })
}
Xerophthalmia answered 27/8, 2018 at 8:19 Comment(1)
I wish it didn't have a syntax error in the code snippet to be able to see the result.Bushy
M
14

You are just binding function and not calling it.

The right synatx to use bind and called binded function.

if (window.confirm("Delete the item?")) {
    let removeToCollection = this.removeToCollection.bind(this, 11);//bind will return to reference to binded function and not call it.
    removeToCollection();
}

OR you can do like this as well without bind.

if (window.confirm("Delete the item?")) {
  this.removeToCollection(11);
}

If this is concern inside removeToCollection then use arrow function to define it.

removeToCollection=(key)=> {
    console.log(key);
  }

Working codesandbox demo

Managerial answered 27/8, 2018 at 8:22 Comment(0)
P
8

I did the same as below-

I have a smart(class) component

<Link to={`#`} onClick={() => {if(window.confirm('Are you sure to delete this record?')){ this.deleteHandler(item.id)};}}> <i className="material-icons">Delete</i> </Link>

I defined a function to call the delete endpoint as-

deleteHandler(props){
    axios.delete(`http://localhost:3000/api/v1/product?id=${props}`)
    .then(res => {
      console.log('Deleted Successfully.');
    })
  }

And that worked for me!

Pellikka answered 25/4, 2019 at 13:56 Comment(0)
G
-1
window.confirm();

truly works. Dont know why confirm(); alone doesn't work on react

Gordon answered 30/3, 2024 at 16:6 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.