Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional rendering in react js(Condition inside one more condition)

Tags:

reactjs

Actually i'm trying to hide and show some html elements in react. I have a created a employee form in which we have multiple partitions for example personal information, contact information etc... I created next and previous buttons in form to show and hide other partitions in the form There is a partition in which we have education details to fill, so i chose to keep a plus and minus button to add education details using conditions.

But finally, my problem is like i have complex conditions on **render** function:

{someCondition && <div>
{anotherCondition && <div></div>}
</div>}

But, i'm getting: *Adjacent JSX elements must be wrapped in an enclosing tag error*. So, what i'm supposed to do? 
like image 606
Manikanta B Avatar asked Sep 10 '25 20:09

Manikanta B


2 Answers

You have to wrap them in a div first. Also, your syntax looks a bit wrong (you missed a closing }).

{someCondition && (
  <div>
    {anotherCondition && <div></div>}
  </div>
)}
like image 64
mersocarlin Avatar answered Sep 15 '25 04:09

mersocarlin


From the code you gave, I believe you need an enclosing tag like this:

//div wraps everything else so the component can render.
<div>
{someCondition && <div>
{anotherCondition && <div></div>
</div>}
</div>
like image 34
Dream_Cap Avatar answered Sep 15 '25 03:09

Dream_Cap



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!