Home > Software engineering >  List Item is Still visible even after deleting from array as well as local storage
List Item is Still visible even after deleting from array as well as local storage

Time:06-04

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;
 

CodePudding user response:

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);
      };

  }

CodePudding user response:

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);
  };

CodePudding user response:

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

  • Related