Here's my example code (I'm trying in a separated component with different Data.
import React, { useState } from 'react';
const initialState = [
{ id: 0, text: 'add to cart' },
{ id: 1, text: 'add to cart' },
{ id: 2, text: 'add to cart' },
];
const Test: React.FC = () => {
const [text, setText] = useState(initialState);
const handleClick = (index: number) => {
const newText = [...text];
newText[index].text = 'Added to cart...';
setText(newText);
setTimeout(() => {
setText(initialState);
}, 2000);
};
return (
<div>
{text.map((buttons, index) => (
<button key={index} onClick={() => handleClick(index)}>
{buttons.text}
</button>
))}
</div>
);
};
export default Test;