【问题标题】:Confirm Window in ReactReact 中的确认窗口
【发布时间】:2019-02-01 17:30:35
【问题描述】:

我有以下代码:

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>
  )
 })
}

我也有这个功能:

removeToCollection(key, e) {

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

当我在“onclick”按钮中使用没有确认窗口的功能时,代码运行良好。但是当我想使用确认窗口时,当我点击我的按钮时会显示确认窗口,但我的项目没有被删除。

有什么想法吗?

感谢您的帮助!

【问题讨论】:

    标签: javascript reactjs firebase firebase-realtime-database confirm


    【解决方案1】:

    我做了和下面一样的-

    我有一个智能(类)组件

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

    我定义了一个函数来调用删除端点——

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

    这对我有用!

    【讨论】:

      【解决方案2】:

      你只是绑定函数而不是调用它。

      使用bind 的正确语法并调用binded 函数。

      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();
      }
      

      或者你也可以这样做而不用绑定。

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

      如果 thisremoveToCollection 内部的关注点,则使用 arrow function 来定义它。

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

      工作codesandbox demo

      【讨论】:

        【解决方案3】:

        基本上你是绑定函数而不是调用它......你应该事先绑定,最好在构造函数中......然后调用它。 试试这个:

        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>
            )
          })
        }

        【讨论】:

          猜你喜欢
          • 2011-06-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-09-10
          • 1970-01-01
          • 1970-01-01
          • 2013-04-28
          • 1970-01-01
          相关资源
          最近更新 更多