I have buttons iteratively rendered from their parent. I want to change each button's text individually on click. Code as follows:
function App() {
const [btnText, setBtnText] = useState('Add to cart');
const handleClick = () => {
setBtnText('Remove from cart');
};
const btnTexts = [
{
id: 1,
btnText: btnText
},
{
id: 2,
btnText: btnText
}
]
const renderBtns = () => {
btnTexts.map((b) => {
return(
<button onClick={handleClick}>{b.btnText}</button>
)
})
}
return (
<div className="App">
{renderCards(cardDetails)}
</div>
);
}
Initially both buttons show text: "Add to cart". When clicking on a button, both buttons' text change to "Remove from cart". I understand this is due to both buttons' have the same state, but how can I make only the clicked button show "Remove from cart"?