#How am I gonna pass Condition in my .map function?
37 messages · Page 1 of 1 (latest)
want to add more style on condition in li tag
is there any way?
How am I gonna pass Condition in my .map function?
Hi! please refer to this link:
It's make sense
can you ckeck
this
{transaction.map((money: Bill, key: number) => (
<ul className={classes.ul} key={key}>
if (money.amount = +amount) {
<li className={cx(classes.li, classes.income)}>
<span>{money.reason}</span><span>{money.amount}</span>
</li>
} else {
<li className={cx(classes.li, classes.expanse)}>
<span>{money.reason}</span><span>{money.amount}</span>
</li>
}
</ul>
))}
the way I did is right or not
No
inside the If statement
You have a calculation.
It should be a condition;
if (condition) {
// block of code to be executed if the condition is true
}
What are you trying to achieve?
if (value = +value){border green} else {border red}
if value start with + operator
if value start with - operator
yup
if so, you want to give different classes to the element
yup'
Do you want me to write the code and explain it? Or would you rather have me go through every step with you?
I just want the code
I just wanna know how am I gonna pass condition in it
Okay
{transaction.map((money: Bill, index: number) => (
<ul className={classes.ul} key={index}>
<li className={cx(classes.li, money.amount > 0 ? classes.income : classes.expanse )}>
<span>{money.reason}</span><span>{money.amount}</span>
</li>
</ul>
))}
Inside the cx function I added
money.amount > 0 ? classes.income : classes.expanse
This is a javascript ternary operator used to shorten if statements
condition ? ifTrue : ifFalse
So the condition is money.amount > 0 this checks if it's higher than 0. So negative values will return false and positive will return true
So if money.amount is greater than 0, add classes.income. if not, add classes.expanse
Also, expanse needs to be expense