I have an array of objects prjLvlData
. This array contains information that I use to conditionally render a component.
In my .jsx file I have
{
prjLvlData.map(ele => {
if (ele.fields.level_exp === 'portfolio' && ele.fields.has_experience) {
return <div className="btn btnAddBadge button noCursor projectLevelBtn active" id={`btnPortfolio${id}`}>Portfolio</div>
} else {
return <div className="btn btnAddBadge button noCursor projectLevelBtn" id={`btnPortfolio${id}`}>Portfolio</div>
}
if (ele.fields.level_exp === 'program' && ele.fields.has_experience) {
return <div className="btn btnAddBadge button noCursor projectLevelBtn active" id={`btnProgram{id}`}>Program</div>
} else {
return <div className="btn btnAddBadge button noCursor projectLevelBtn " id={`btnProgram{id}`}>Program</div>
}
if (ele.fields.level_exp === 'project' && ele.fields.has_experience) {
return <div className="btn btnAddBadge button noCursor projectLevelBtn active" id={`btnProject${id}`}>Project</div>
} else {
return <div className="btn btnAddBadge button noCursor projectLevelBtn" id={`btnProject${id}`}>Project</div>
}
})
}
How can I build them so that by the end of the 3 if
statements, I return the built-up component, instead of returning inside every if
statement?
I want something like this, in pseudo code
var component = <div>
if (...)
component += thing1
if (...)
component += thing2
return component + <div>