简体   繁体   中英

React+Typescript conditional rendering

I want to create an option for every single profile that I have. I checked and I do have a profile so the profiles array is not empty and profile.profilename isn't undefined. It just doesn't render.

What i see: Picture

<div className="col-4">
   <select name="profile" className="billingSelector">
         <option>Billing profile</option>
             {(getProfiles() as any).forEach((profile:Profile) => {
                  <option>{profile.profileName}</option>
              })}
   </select>
</div>

I found a solution. I used map instead of foreach and i removed curly braces and used regular ones.

<select name="profile" className="billingSelector">
   <option>Billing profile</option>
   {getProfiles().map((profile:Profile)=> (
        <option key={profile.profileName}>{profile.profileName}</option>
   ))}       
</select>

Use map instead of foreach .

There is a difference. Map returns list, foreach - not.

More info on that:

Array.prototype.forEach() - JavaScript | MDN

Array.prototype.map() - JavaScript | MDN

Instead of foreach can you please try map? Eg

getProfiles().map((profile, index) => ( {profile.profileName}));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM