简体   繁体   English

如何在反应应用程序中从实时 Firebase 中删除单个项目?

[英]How to delete single item from realtime Firebase in react app?

can someone help me how I can make to delete a single item from realtime database using remove() function following this code?有人可以帮助我如何使用 remove() function 按照此代码从实时数据库中删除单个项目吗? It say's that 'id' is not defined Thanks )它说'id'没有定义谢谢)

import { expensesRef } from '../firebase';

const ExpenseItem = (props) => {

    const handleDeleteExpense = (id) => {
        
        expensesRef.child(`expenses/` + id).remove();
    };

    return (
        <li class='list-group-item d-flex justify-content-between align-items-center'>
            {props.name}
            <div>
                <span class='badge badge-success mr-3'>{props.priority}</span>
                <span class='badge badge-info mr-3'>{props.cost} Lei</span>
                <TiDelete size='1.5em' onClick={handleDeleteExpense(`${id}`)} />
            </div>
        </li>
    );
};

export default ExpenseItem;

这是我的数据库的外观

For the sake of explanation, I'm going to remove the handleDeleteExpense function from your code, leaving it with:为了便于解释,我将从您的代码中删除handleDeleteExpense function,留下:

const ExpenseItem = (props) => {
    return (
        <li class='list-group-item d-flex justify-content-between align-items-center'>
            {props.name}
            <div>
                <span class='badge badge-success mr-3'>{props.priority}</span>
                <span class='badge badge-info mr-3'>{props.cost} Lei</span>
                <TiDelete size='1.5em' onClick={handleDeleteExpense(`${id}`)} />
            </div>
        </li>
    );
};

You see here that, the only spot referencing id is in the TiDelete component.您在这里看到,唯一的点引用idTiDelete组件中。 Most likely, you intended id to be set by the parent component like you are doing for name , priority and cost .最有可能的是,您希望id由父组件设置,就像您为nameprioritycost所做的那样。 So you need to change id to props.id .因此,您需要将id更改为props.id

Then in the parent component, you'd add it using something similar to:然后在父组件中,您将使用类似于以下内容的方式添加它:

const { name, priority, cost } = ref.val();
<ExpenseItem
  id=snapshot.key
  name=name
  priority=priority
  cost=cost
/>

or the shorter或更短的

<ExpenseItem
  { ...snapshot.val() }
  id=snapshot.key
/>

It's important to note that the snapshot's key, not your id inside your data, is what is being passed through.重要的是要注意快照的密钥,而不是您数据中的 id,是正在传递的内容。 If you used the other ID, the handleDeleteExpense isn't going to work.如果您使用了另一个 ID, handleDeleteExpense将不起作用。

Next, this line, will cause your data to be deleted as soon as your component renders because you are calling the function straight away:接下来,这一行将导致您的数据在您的组件呈现后立即被删除,因为您正在立即调用 function:

<TiDelete size='1.5em' onClick={handleDeleteExpense(`${id}`)} />

Instead it should be:相反,它应该是:

<TiDelete size='1.5em' onClick={() => handleDeleteExpense(`${id}`)} />

You also import a reference called expensesRef , which I assume has been initialized with firebase.database().child('expenses') .您还导入了一个名为expensesRef的引用,我假设它已使用firebase.database().child('expenses')进行了初始化。 This means your delete instruction is also deleting expenses/expenses/<id> instead of expenses/<id> .这意味着您的删除指令也在删除expenses/expenses/<id>而不是expenses/<id>

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

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