Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

react hooks delete item from array

import React, { useState } from "react";
import ReactDOM from "react-dom";

import "./styles.css";

const App = () => {
  const defaultList = [
    { name: "ItemOne" },
    { name: "ItemTwo" },
    { name: "ItemThree" }
  ];

  const [list, updateList] = useState(defaultList);

  const handleRemoveItem = (e) => {
   const name = e.target.getAttribute("name")
    updateList(list.filter(item => item.name !== name));
  };

  return (
    <div>
      {list.map(item => {
        return (
          <>
            <span name={item.name} onClick={handleRemoveItem}>
              x
            </span>
            <span>{item.name}</span>
          </>
        );
      })}
    </div>
  );
};

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
Comment

usestate array delete

const removeProduct = (index) => {
  setProductsArray([
    ...products.slice(0, index),
    ...products.slice(index + 1, products.length)
  ]);
}
Comment

usestate remove from array

  const defaultList = [
    { name: "ItemOne" },
    { name: "ItemTwo" },
    { name: "ItemThree" }
  ];

  const [list, updateList] = useState(defaultList);

  const handleRemoveItem = (e) => {
   const name = e.target.getAttribute("name")
    updateList(list.filter(item => item.name !== name));
  };
Comment

remove element from array in usestate

 let index = array.indexOf(e.target.value)
   if (index !== -1) {
      array.splice(index, 1);
      setState(array);
   }
Comment

react hooks remove item from array

{

cards:[

"id":"1",
      "name":"something"

   ]

}
Comment

PREVIOUS NEXT
Code Example
Javascript :: input field take only number and one comma 
Javascript :: vanilla javascript add class 
Javascript :: moment js - to find if dates are same by day 
Javascript :: npm http status codes 
Javascript :: js two array combining with id 
Javascript :: install nodemon 
Javascript :: how to fix header on scroll 
Javascript :: jquery window trigger resize 
Javascript :: call laravel route js 
Javascript :: ReferenceError: primordials is not defined 
Javascript :: iframe content fetching 
Javascript :: i18n react meta description 
Javascript :: How to remove title in material-table 
Javascript :: alphabet only in jquery 
Javascript :: jquery find children not working 
Javascript :: new line in react js 
Javascript :: how to hash with crypto Node.js 
Javascript :: discord.js v13 client 
Javascript :: jquery get left position 
Javascript :: redux devtools 
Javascript :: sls invoke local 
Javascript :: react toastify does not have design 
Javascript :: mongoose validate on update 
Javascript :: typescript how to mode json files when compile 
Javascript :: javascript on keypu 
Javascript :: How to Perform Date Comparison With the Date Object in JavaScript 
Javascript :: moment cdn 
Javascript :: stylelint default config 
Javascript :: javascript loop through child elements 
Javascript :: us phone number regex 
ADD CONTENT
Topic
Content
Source link
Name
1+8 =