I ran into a problem where filter is not removing the matched item, instead the last element of the
Unanswered
Asiatic Lion posted this in #help-forum
Asiatic LionOP
array. i tried using both content (which is Date.now()) and index, yet it still removed the last element of the array
const [inputs, setInputs] = useState<number[]>([Date.now()]);
{inputs.map((input, index) => (
<div
key={index}
id={String(index)}
className="flex items-end gap-4 space-y-1"
>
onClick={() =>
setInputs((prev) =>
prev.filter(
(innerInput, innerIndex) => innerIndex !== index
3 Replies
@Asiatic Lion array. i tried using both content (which is Date.now()) and index, yet it still removed the last element of the array
const [inputs, setInputs] = useState<number[]>([Date.now()]);
{inputs.map((input, index) => (
<div
key={index}
id={String(index)}
className="flex items-end gap-4 space-y-1"
>
onClick={() =>
setInputs((prev) =>
prev.filter(
(innerInput, innerIndex) => innerIndex !== index
you need a stable reference for the key if you want to dynamically alter arrays in react
can't use index
@alfonsüs ardani can't use index
Asiatic LionOP
i tried using the id earler and it still didnt work