简体   繁体   中英

How can I change the style after mapping the array with js?

I would like to change the color to green of the element "tr.amount" if its > 0

I tried with the code below but it gives me this error: Uncaught TypeError: Cannot set properties of undefined (setting 'color')

Thanks in advance!

{userData.transactions.length > 0 &&
   userData.transactions.map((tr) => (
       <tr className="border">
         <td> <span className="history-category">{tr.category}</span></td>
         <td className="amount">{tr.amount > 0 ? tr.amount.style.color = "green" : tr.amount.style.color = "red"}</td>
         <td>{tr.date}</td>
       </tr>
))}

You probably want to use the style property of the td itself. Something in lines of

<td className="amount" style={{ color: tr.amount > 0 ? "green" : "red" }}>
  My Content
</td>

你有tr.amount.style.color而不是tr.style.color

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM