Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

137
Visualizações
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? It say's that 'id' is not defined Thanks )

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;

Here is how my database looks

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

For the sake of explanation, I'm going to remove the handleDeleteExpense function from your code, leaving it with:

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. Most likely, you intended id to be set by the parent component like you are doing for name, priority and cost. So you need to change id to 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. If you used the other ID, the handleDeleteExpense isn't going to work.

Next, this line, will cause your data to be deleted as soon as your component renders because you are calling the function straight away:

<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'). This means your delete instruction is also deleting expenses/expenses/<id> instead of expenses/<id>.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda