Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If condition inside of map() React

Tags:

reactjs

I have a map()function that needs to display views based on a condition. I've looked at the React documentation on how to write conditions and this is how you can write a condition:

{if (loggedIn) ? (   // Hello! ) : (   // ByeBye! )} 

Here's the link: https://facebook.github.io/react/docs/conditional-rendering.html#inline-if-else-with-conditional-operator

So, I tried to take that knowledge and implemant it in my React app. And it turned out like this:

render() {   return (     <div>       <div className="box">         {this.props.collection.ids           .filter(             id =>               // note: this is only passed when in top level of document               this.props.collection.documents[id][                 this.props.schema.foreignKey               ] === this.props.parentDocumentId           )           .map(id =>             {if (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>             )}           )}       </div>     </div>   ) } 

But it doesn't work..! Here's the error:

Screenshot

I appreciate all the help I can get!

like image 899
Martin Nordström Avatar asked Jul 07 '17 11:07

Martin Nordström


People also ask

How do you write if condition inside a map in React?

To use a condition inside map() in React:Call the map() method on an array. Use a ternary operator to check if the condition is truthy. The operator returns the value to the left of the colon if the condition is truthy, otherwise the value to the right is returned.

Can we use if-else inside JSX?

if-else statements don't work inside JSX. This is because JSX is just syntactic sugar for function calls and object construction.

Can we use map inside map in react JS?

Use a map() inside a map() function in React # To use a map() inside a map() function in React: Call map() on the outer array, passing it a function. On each iteration, call the map() method on the other array.


2 Answers

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> } 
like image 64
Mayank Shukla Avatar answered Oct 05 '22 02:10

Mayank Shukla


There are two syntax errors in your ternary conditional:

  1. remove the keyword if. Check the correct syntax here.
  2. 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!

like image 41
dhaliman Avatar answered Oct 05 '22 01:10

dhaliman