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 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 :: javascript seconds to date 
Javascript :: mongoose find and update prop 
Javascript :: cypress have attribute 
Javascript :: phaser 3 add button 
Javascript :: toggle css class in javascript 
Javascript :: newtonsoft.json string to object 
Javascript :: javascript filter unique 
Javascript :: javascript on image load 
Javascript :: white screen issue in react native splashscreen 
Javascript :: how to get current date in react js 
Javascript :: string replace in javascript 
Javascript :: update angular materia; 
Javascript :: string.find javascript 
Javascript :: jquery external script 
Javascript :: jquery search for string in text 
Javascript :: remove axis tick ends d3 
Javascript :: jquery check if type is checkbox 
Javascript :: Vuejs v-model when enter pressed 
Javascript :: livewire upload progress 
Javascript :: node uuid 
Javascript :: find the max length of string elements in an array 
Javascript :: react native get mac address 
Javascript :: check if input is a number javascript 
Javascript :: write bytes64 in json python 
Javascript :: js json to object 
Javascript :: get value by name array from select in jquery 
Javascript :: js append en tête 
Javascript :: jquery each loop 
Javascript :: how to get value in formgroup in angular 
Javascript :: check device in flutter 
ADD CONTENT
Topic
Content
Source link
Name
2+9 =