I'm trying do a conditional render in react, but isn't work.
That is my function:
populateBodyInstallationsEdition() {
letshow = null;
console.log(this.state.installations);
for (let i = 0; i < this.state.IdInstallGroup.length; i++) {
for (let j = 0; j < this.state.installations.length; j++) {
console.log(this.state.IdInstallGroup[i]);
console.log(this.state.installations[j].id);
if (this.state.IdInstallGroup[i] === this.state.installations[j].id) {
show = (
<div>
<div>
<input
type="checkbox"
value="None"
className="test"
id={this.state.installations[j].id}
name={this.state.installations[j].Name}
checked
/>
{/* <label htmlFor={arr[index].id} /> */}
</div>
<label htmlFor={this.state.installations[j].id}>
{this.state.installations[j].Name}
</label>
</div>
);
} else {
show = (
<div>
<div>
<input
type="checkbox"
value="None"
className="test"
id={this.state.installations[j].id}
name={this.state.installations[j].Name}
/>
{/* <label htmlFor={arr[index].id} /> */}
</div>
<label htmlFor={this.state.installations[j].id}>
{this.state.installations[j].Name}
</label>
</div>
);
}
}
}
return show;
}
the conditions are making the right comparisons but don't rende the JSX
what could be wrong ?
how can i solve this problem ?
showconst but usingthis.show. I would say that you need to replacethis.showwithshowIdInstallGroupandinstallations. If you want to render one single value, find it first and then render it. If you want to render a list of items, you need to usemapand return an array of elements. The iteration overIdInstallGroupshould be probably removed and replaced with a singleincludescheck.