0
items.map((itemsvalue, index) => {
  return (
    <>
      <li key={index}>
        <button className="deleteBtn" onClick={DeleteBtn}>
          X
        </button>
        {itemsvalue}
      </li>
    </>
  );
});

Here items is an array of strings. How can i access index value in the DeleteBtn function???

Behemoth
  • 5,389
  • 4
  • 16
  • 40

2 Answers2

0

You can pass an inline function for the onClick handler. I have changed function name to onClickDelete , because its a common practice to name the click handlers as onClickDelete, handleDelete .

items.map((itemsvalue, index) => {
  return (
    <>
      <li key={index}>
        <button className="deleteBtn" onClick={() => onClickDelete(index)}>
          X
        </button>
        {itemsvalue}
      </li>
    </>
  );
});
Shyam
  • 5,292
  • 1
  • 10
  • 20
-1

You can pass index as the second argument in map

MDN lINK

ME-ON1
  • 83
  • 1
  • 10