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

127
Visualizações
List Item is Still visible even after deleting from array as well as local storage

I have a button named yes in the child component where I delete a list item from array and local storage using the props.id passed from the parent component.

The problem is the item is deleted from array and local storage but is still visible on the screen until I press delete button on another item in the parent component.

when I press delete button in the parent component it opens an overlay. When I press yes button on overlay I want list item to be removed from the screen immediately.

here is the code in the child component.

       import React, { useCallback, useState } from "react";
import styles from "./Delete.module.css";

function Delete(props) {
  //  console.log();

  const store = props.store;
  const [no, setNo] = useState(false);
  let [deleted, setDelted] = useState(store);

  console.log(deleted);

  console.log("Length :" + store.length);
  const noBtnHandler = () => {
    console.log("clicked");
    setNo(!no);
    props.setDel(false);
  };

  const yesBtnHandler = () => {
    console.log("Dlete.js :" + props.id);
    const filteredStore = deleted.filter((task) => task.id !== props.id);
    setDelted([...filteredStore]);
    localStorage.removeItem(props.id);
    //   console.log(deleted);

    setNo(!no);
  };

  return (
    <div className={`${no === false ? styles.del : styles.delOn}`}>
      <section>
        <div>
          <h3>Are you Sure ?</h3>
        </div>
        <div>
          <button type="button" onClick={yesBtnHandler}>
            {" "}
            Yes{" "}
          </button>
          <button type="button" onClick={noBtnHandler}>
            {" "}
            No{" "}
          </button>
        </div>
      </section>
    </div>
  );
}

export default Delete;
 
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Try remove the trailing ...

const yesBtnHandler = () => {
    console.log("Dlete.js :" + props.id);
    const filteredStore = deleted.filter((task) => task.id !== props.id);
    setDelted([filteredStore]);
    //or setDelted(filteredStore);
    localStorage.removeItem(props.id);
    //   console.log(deleted);

    setNo(!no);
  };
about 4 years ago · Juan Pablo Isaza Relatório

0

You are passing store from the parent component to the Delete Component and setting a new state here 'deleted'. so you are only calling the setDeleted on the Delete component which wont affect the parent component.

The correct implementation is to have the store state in the parent component if you don't already have it. It is will still be same like deleted state but possibly with a better name. Say const [store, setStore] = useState([])

Define a function to filter out a particular record just like you have in the yesBtnHandler handler. but this function will be defined in the parent component. Say as an example

 const removeRecord = (id)  => {
      const filteredStore = store.filter((task) => task.id !== id);
      localStorage.removeItem(id);
      setStore(filteredStore);
 }

You now need to pass the a function to the Delete Component from the parent rather than passing the whole store. Like

  <Delete removeFunc= {() => { removeRecord(id) }} />

After passing this function, you need to call it in your yesBtnHandler function. Like

  function Delete({removeFunc}) {

     ...

     const yesBtnHandler = () => {
          removeFunc();
          setNo(!no);
      };

  }
about 4 years ago · Juan Pablo Isaza Relatório

0

my understanding of this is that you're trying to change the state of a parent component from a child component. If that's what you're intending to do then you can do the following:

  1. Define the function Delete(id) {...} inside the parent component rather than the child component.
  2. Next, you'll have to pass both the function and the array to your child component, something like this: <ChildComponent array={array} onDelete={Delete}, where array is the array in your parent component and Delete is the function to delete an item from the array.
  3. Finally, in your child component, with the props passed in correctly, i.e, function ChildComponent({array, Delete}) {...}you can now have access to the array in your parent component, and actually modify it like you'd like. To fire the event listener on the yes button in the child component, do this: <button type="button" onClick={() => Delete(array.id)}> {" "} Yes{" "} </button>

I hope this will be helpful

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