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

react hooks remove item from array

{

cards:[

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

   ]

}
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript string to date format dd/mm/yyyy 
Javascript :: why android folder size is 500mb in react native 
Javascript :: material ui refresh icon 
Javascript :: code to sum of specific nodes in binary tree for int kDistancefrom node(struct Tree,int k,int n); 
Javascript :: node javascript retry promise.all 
Javascript :: counter random interval 
Javascript :: javascript is even 
Javascript :: JavaScript URL Parse including pathname 
Javascript :: Custom usePagination hook 
Javascript :: Rest and spread operators in ES6 
Javascript :: JS get dropdown setting 
Javascript :: react clearinterval outside of useefect 
Javascript :: javascript responsive carousel 
Javascript :: get text 
Javascript :: react native red circle bubble 
Javascript :: what is the maximum x value of a window for mouse listener 
Javascript :: top-level await 
Javascript :: jshack1 
Javascript :: hide fill apexcharts 
Javascript :: automatically function run js function on load after some time 
Javascript :: react call component state 
Javascript :: initialize back4app 
Javascript :: lodash uniqBy alterantive in js 
Javascript :: dev console with colored font 
Javascript :: contoh penggunaan promise 
Javascript :: filter syntax 
Javascript :: how to save js object to clipboard 
Javascript :: history go back js oneline 
Javascript :: javascript array filter exercises 
Javascript :: jquery element by name 
ADD CONTENT
Topic
Content
Source link
Name
5+2 =