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

PREVIOUS NEXT
Code Example
Javascript :: how to convert python code to javascript 
Javascript :: js pipe 
Javascript :: convert string to number 
Javascript :: javascript tree search 
Javascript :: password generator javascript 
Javascript :: js 1 second sleep 
Javascript :: editor convert jquery code to javascript 
Javascript :: convert 12 hour to 24 hour javascript 
Javascript :: new react 
Javascript :: dynamic styles in react native 
Javascript :: how to validate multiple input field in javascript 
Javascript :: comment field react 
Javascript :: open json javascript 
Javascript :: nextjs apollo client 
Javascript :: path js 
Javascript :: javascript timer countdown with seconds 59 
Javascript :: shopify liquid logic 
Javascript :: inappbrowser hide url 
Javascript :: JavaScript for loop Display a Text Five Times 
Javascript :: javascript Add Symbol as an Object Key 
Javascript :: javascript for...of with Generators 
Javascript :: javaScript values() Method 
Javascript :: jsonformat iso 8601 
Javascript :: Remove key from obj and save in diff obj 
Javascript :: alternative for react-tilt 
Javascript :: phaser rotate around distance 
Javascript :: get lat long react native 
Javascript :: JS table with rows that have alternating colours 
Javascript :: efectos javascript 
Javascript :: js replay animation 
ADD CONTENT
Topic
Content
Source link
Name
6+8 =