Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
174
rated 0 times [  178] [ 4]  / answers: 1 / hits: 18349  / 6 Years ago, wed, july 4, 2018, 12:00:00

i have working code and little problem with if statement in map function here is code



    const SortableItem = SortableElement(CashItem);
const SortableList = SortableContainer(({items}) => {
return (
<div>
{items.map((cashitem, index) => (
<SortableItem key={`item-${index}`}
index={index}
isPayed={cashitem.isPayed}
date={cashitem.date}
name={cashitem.name}
realisticVal={cashitem.realisticVal}
realisticBalance={cashitem.realisticBalance}
optimisticBalance={cashitem.optimisticBalance}
optimisticVal={cashitem.optimisticVal}
changedName={(event) => this.nameChangedHandler(event, cashitem.id)}
isBlocked={(event) => this.handleChangeCheckbox(event, cashitem.id)}
delete={(event) => this.removeItem(event, cashitem.id)}
addNext={(event) => this.addNext(event, cashitem)}
realisticChange={(event) => this.realisticBalanceChangedHandler(event, cashitem.id)}
optimisticChange={(event) => this.optimisticBalanceChangedHandler(event, cashitem.id)}
dateChangedHandler={(event) => this.dateChangedHandler(event, cashitem.id)}
/>
))}
</div>
);
});


now i want chceck if statement to render only when in map cashitem has state is visible cashitems isVisible already have isVisible:true or false i want to do something like that



 const SortableItem = SortableElement(CashItem);
const SortableList = SortableContainer(({items}) => {
return (
<div>
{items.map((cashitem, index) => (
if(cashitem.isVisible===true){
<SortableItem key={`item-${index}`}
index={index}
isPayed={cashitem.isPayed}
date={cashitem.date}
name={cashitem.name}
realisticVal={cashitem.realisticVal}
realisticBalance={cashitem.realisticBalance}
optimisticBalance={cashitem.optimisticBalance}
optimisticVal={cashitem.optimisticVal}
changedName={(event) => this.nameChangedHandler(event, cashitem.id)}
isBlocked={(event) => this.handleChangeCheckbox(event, cashitem.id)}
delete={(event) => this.removeItem(event, cashitem.id)}
addNext={(event) => this.addNext(event, cashitem)}
realisticChange={(event) => this.realisticBalanceChangedHandler(event, cashitem.id)}
optimisticChange={(event) => this.optimisticBalanceChangedHandler(event, cashitem.id)}
dateChangedHandler={(event) => this.dateChangedHandler(event, cashitem.id)}
/>
}

))}
</div>
);
});

More From » reactjs

 Answers
3
{
items.map((cashitem, index) => { //<== change it to curly braces

return cashitem.isVisible && (<SortableItem key={`item-${index}`} //<== using the return keyword
...
/>)

}
}

Instead of using parenthesis, change it to curly braces and using a return keyword withint the if else condition as above


[#54057] Saturday, June 30, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
lindsay

Total Points: 402
Total Questions: 109
Total Answers: 109

Location: Tuvalu
Member since Sat, Feb 11, 2023
1 Year ago
;