“usestate hapus dari array” Kode Jawaban

Usebah hapus array

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

react hooks menghapus item dari 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);
Busy Buzzard

usestate hapus dari 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));
  };
shahul

Hapus elemen dari array di Usestate

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

Jawaban yang mirip dengan “usestate hapus dari array”

Pertanyaan yang mirip dengan “usestate hapus dari array”

Lebih banyak jawaban terkait untuk “usestate hapus dari array” di JavaScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya