Home > Net >  How to remove the particular data using with id
How to remove the particular data using with id

Time:12-06

This is my data

setRepairServicedata([{
        id: 1,
        image: "/assets/images/repair_service/charger.png",
        name: "Battery",
        mrp: "950.8 AED",
        sale_price: "950 AED",
      },
      {
        id: 2,
        image: "/assets/images/repair_service/auxjack.png",
        name: "Aux Jack",
        mrp: "950.1 AED",
        sale_price: "951 AED",
      },
      {
        id: 3,
        image: "/assets/images/repair_service/priomirty.png",
        name: "Proximity Sensor",
        mrp: "950.9 AED",
        sale_price: "952 AED",
      },
    ]);
    <Button
            className={Styles.remvbtn}
            onClick={() => handleaddnowFunc(did)}
          >
            Remove
          </Button>
  const handleremvFunc = (did) => {
    
  };

How to remove the particular data in the state. for eg if button have id 2 i need to delete the particular data in this (setRepairServicedata) state.

CodePudding user response:

use the filter method

  const handleremvFunc = (did) => {
     setRepairServicedata(prev => prev.filter(item => item.id !== did))
  };
  • Related