0

I tried using if/else and same problem JSX

return ( 
     <Layout>
           <>
            {data.map((service, index) => (
              { index % 2 === 0 ?
                 <div className="circle"></div>
                 :
                 <div className="square"></div>  
               } 
              ))
             }
        </>
      </Layout> 
  )

ERROR

enter image description here

1
  • I think that you need to remove the parenthesis after the fat arrow Commented Feb 3, 2020 at 23:00

3 Answers 3

1

Please use below code.

return ( 
     <Layout>
           <>
            {data.map((service, index) => {
              return index % 2 === 0 ?
                 <div className="circle"></div>
                 :
                 <div className="square"></div>  

              })
             }
        </>
      </Layout> 
  )
Sign up to request clarification or add additional context in comments.

Comments

1

For a much cleaner alternative:

  return ( 
     <Layout>
       {data.map((service, index) => (
         <div className={index % 2 === 0 ? 'circle' : 'square'}></div>
       ))}
      </Layout> 
  )

3 Comments

I guess i didn't put the full code, but the problem is that the content structure on the inside needs to alternate but based on my example this is correct
To add, this is perfect if you only wanted to toggle css classes
I actually ended up doing somethign toggleing classes and handeling stuff on the scss side but i wanted to do it how to do it without
1

What about this:

const elementSet = data.map(( service, index) => ((index % 2)===0) ? (<div className="circle"></div>) : (<div className="square"></div>) );
return ( 
       <Layout><>
        {elementSet}
       </></Layout>
);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.