Lit*_*Boy 2 javascript confirm firebase reactjs firebase-realtime-database
我有以下代码:
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>
)
})
}
Run Code Online (Sandbox Code Playgroud)
我也有这个功能:
removeToCollection(key, e) {
const item = key;
firebase.database().ref(`catalogue/${item}`).remove();
}
Run Code Online (Sandbox Code Playgroud)
当我在“ onclick”按钮中使用没有确认窗口的功能时,代码会很好用。但是,当我要使用确认窗口时,单击我的按钮时会显示确认窗口,但是我的项目没有被删除。
任何的想法 ?
感谢您的帮助 !
基本上,您是在绑定函数而不是调用它……您应该预先绑定,最好是在构造函数中……然后调用它。尝试这个:
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>
)
})
}Run Code Online (Sandbox Code Playgroud)
你只是绑定函数而不是调用它。
要使用的正确语法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();
}
Run Code Online (Sandbox Code Playgroud)
或者你也可以在没有绑定的情况下这样做。
if (window.confirm("Delete the item?")) {
this.removeToCollection(11);
}
Run Code Online (Sandbox Code Playgroud)
如果这是内部问题,removeToCollection则使用arrow function来定义它。
removeToCollection=(key)=> {
console.log(key);
}
Run Code Online (Sandbox Code Playgroud)
在职的 codesandbox demo
我做了与下面相同的事情-
我有一个智能(类)组件
<Link to={`#`} onClick={() => {if(window.confirm('Are you sure to delete this record?')){ this.deleteHandler(item.id)};}}> <i className="material-icons">Delete</i> </Link>
Run Code Online (Sandbox Code Playgroud)
我定义了一个函数来调用删除端点为-
deleteHandler(props){
axios.delete(`http://localhost:3000/api/v1/product?id=${props}`)
.then(res => {
console.log('Deleted Successfully.');
})
}
Run Code Online (Sandbox Code Playgroud)
这对我有用!
| 归档时间: |
|
| 查看次数: |
12207 次 |
| 最近记录: |