Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to use if statement inside React JSX loop/map

I’m trying to output two different versions of some HTML inside a React component based on an if statment, this is how I think it should look but the syntax is not right. I am trying to add "active" Breadcrumb.Item if the value of breadCrumbLink.IsActive is true.

{breadCrumbData.BreadCrumbLinks.map(breadCrumbLink => (

if (breadCrumbLink.IsActive == true)
{
    <Breadcrumb.Item key={breadCrumbLink.Id} className="active">
        {breadCrumbLink.LinkText}
    </Breadcrumb.Item active>

}
else
{
    <Breadcrumb.Item key={breadCrumbLink.Id} className="active">
        {breadCrumbLink.LinkText}
    </Breadcrumb.Item>
}

))}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You don’t need to render conditionally. Use the condition inside the active property (and inside className if you need):

{breadCrumbData.BreadCrumbLinks.map(breadCrumbLink => 
    (
        <Breadcrumb.Item key={breadCrumbLink.Id} className="active" active={breadCrumbLink.IsActive}>
            {breadCrumbLink.LinkText}
        </Breadcrumb.Item>
    )
)}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading