简体   繁体   English

为什么我的删除按钮在React中无法正确呈现?

[英]Why is my remove button not rendering properly in React?

When I click the remove button from a particular item, it removes the last element in the list of items. 当我单击特定项目的删除按钮时,它将删除项目列表中的最后一个元素。 I've used a pop up screen (modal) before deleting the item and after that it is not working properly. 在删除项目之前,我使用了一个弹出屏幕(模式),此后它无法正常工作。 Please find below the modified code. 请在下面找到修改后的代码。 The sample images are attached. 随附示例图像。 When I press the close for rem me the last element is removed and not that element. 当我按下关闭按钮时,最后一个元素被删除,而不是那个元素。 Here is the code for the items: 这是项目的代码:

<tbody>
                            {this.state.dishes ? this.state.dishes.map( (dish) =>
                                <tr class="row100 body" key={dish.id}>
                                    <td class="cell100 column2">{dish.Vendor}</td>
                                    <td class="cell100 column2">{dish.Dish}</td>
                                    <td class="cell100 column3">{dish.Price}</td>
                  <td class="cell100 column1">{dish.Days}</td>
                                    <td class="cell100 column4"><div className="test"><img className='close' onClick={this.onOpenModal} src={ require('./close.png') } />
                                    <Modal open={this.state.open} onClose={this.onCloseModal} showCloseIcon={false} center >
                                        <h2 className='modbtn'>Are you sure you want to remove this dish?</h2>
                                        <Button className="addmenu" onClick={this.remove.bind(this, dish.id)}>Yes</Button>
                                        <Button className="addmenu" onClick={this.onCloseModal}>No</Button>
                                        </Modal>
                                    </div></td>
                                </tr>) : (null) }                               
                            </tbody>

The functions can be found below: 这些功能可以在下面找到:

onOpenModal = () => {
        this.setState({ open: true });
      };
      onCloseModal = () => {
        this.setState({ open: false });
      };
addMessage(e){
        e.preventDefault()

        var newData={
        Vendor: this.inputE3.value,
        Dish: this.inputEl.value,
        Price : this.inputE2.value,
        Days : this.state.days
        }

        fire.database().ref('dishes').push(newData);

      this.inputEl.value = ''; 
      this.inputE2.value = ''; 
      this.inputE3.value = ''; 
      } 
      remove(id){
        // console.log(this.state)
        let a = fire.database().ref('dishes/'+id)
        a.remove()
        this.onCloseModal()
      } 

在此处输入图片说明

在此处输入图片说明

Looks like issue with your function call, But you method accepts one param only. 您的函数调用看起来像问题,但是您的方法仅接受一个参数。

onClick={this.remove.bind( this, dish.id )} onClick = {this.remove.bind( this,dish.id )}

All Firebase database operations are asynchronous and It will be good if you handle it using promises. 所有Firebase数据库操作都是异步的,如果您使用promise处理它,那将是很好的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM