If condition inside of map() React
Solution 1:
You are using both ternary operator
and if
condition, use any one.
By ternary operator:
.map(id => {
return this.props.schema.collectionName.length < 0 ?
<Expandable>
<ObjectDisplay
key={id}
parentDocumentId={id}
schema={schema[this.props.schema.collectionName]}
value={this.props.collection.documents[id]}
/>
</Expandable>
:
<h1>hejsan</h1>
}
By if condition:
.map(id => {
if(this.props.schema.collectionName.length < 0)
return <Expandable>
<ObjectDisplay
key={id}
parentDocumentId={id}
schema={schema[this.props.schema.collectionName]}
value={this.props.collection.documents[id]}
/>
</Expandable>
return <h1>hejsan</h1>
}
Solution 2:
There are two syntax errors in your ternary conditional:
- remove the keyword
if
. Check the correct syntax here. -
You are missing a parenthesis in your code. If you format it like this:
{(this.props.schema.collectionName.length < 0 ? (<Expandable></Expandable>) : (<h1>hejsan</h1>) )}
Hope this works!
Solution 3:
If you're a minimalist like me. Say you only want to render a record with a list containing entries.
<div>
{data.map((record) => (
record.list.length > 0
? (<YourRenderComponent record={record} key={record.id} />)
: null
))}
</div>
Solution 4:
This one I found simple solutions:
row = myArray.map((cell, i) => {
if (i == myArray.length - 1) {
return <div> Test Data 1</div>;
}
return <div> Test Data 2</div>;
});