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>
);
});
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>
);
});
Share
Improve this question
asked Jul 4, 2018 at 8:11
Piotr BieszczadPiotr Bieszczad
1051 gold badge1 silver badge10 bronze badges
1
- use it in map function? – Piotr Bieszczad Commented Jul 4, 2018 at 8:16
4 Answers
Reset to default 8You aren't returning the component inside your if statement.
{items.map((cashitem, index) => {
if(cashitem.isVisible){
return <SortableItem key={`item-${index}`} ...otherProps/>
}
})}
However since you are filtering your list, why not use the Array.filter method?
{items.filter(cashItem => cashItem.isVisible).map((cashitem, index) => (
<SortableItem key={`item-${index}`} ...otherProps/>
)}
{
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
An arrow function which consists of more than a single expression needs:
- Braces around it
- An explicit return statement
Thus:
(cashitem, index) => {
if (condition) {
return <Component />
}
}
Create a function that handle the if/else statement and return the jsx/html builder, then call it in the jsx render() function.
Check this solution/ author: Blair Anderson
renderSkillSection: function(){
if (this.state.challengeChoices.length < 0) {
return this.state.challengeChoices.map((para2, i) =>
<ChallengeSkill key={i} {...para2} callback={this.madeSelection} />)
}
else {
return <div>Hello world</div>
}
},
render: function(){
return (<div className="skillSection">
{this.renderSkillSection()}
</div>)
}