Is it possible to change button text that is created using array. If it is possible, what should I do with the onClick function in the button. Here is the sample code :
function RenderFeeds(props) {
const list = []
props.products.forEach((product) => {
list.push(
<>
<h1>{product.name}</h1>
<h2>{product.company_name}</h2>
<h2>{product.description}</h2>
<button onClick={}>{product.watchlist==false ? 'Add to watchlist':'Remove From watchlist'}</button>
</>
)
})
return (
<>
{list}
</>
)}
So basically I have an array which contains this value :
[{
"name": "lorem ipsum",
"description": "Nam vel nisi rutrum quam ",
"company_name": "PT dapibus ",
"company_slug": "pt-dapibus",
"watchlist": true,
"id": 1
}]
I am creating the component using this array. The watchlist variable will affect the text in the button. If it is true then I want the button to have Remove From Watchlist and viceversa.
I am aware that we can use hook in React and apply useState for onClick button function. But I do not know how do I initialize the state if I use loop to create the component.