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 can I set a key rendering a list in react when two items should be rendered per map iteration?

So I’ve been struggeling with the following issue:
I have a function that returns me a number of years (4 years). These years need to each be mapped with two options in a select ( term 1 and term 2 for example).

 <select>
            {getSemesterSelection().map((el, key) => {
              return (
                <>
                  <option value={`${el}1`} key={`${el}1`}>{`${el}1`}</option>
                  <option value={`${el}2`} key={`${el}2`}>{`${el}2`}</option>
                </>
              );
            })}
          </select>

So react is warning me, that I need to set a key, but I cannot wrap them with a div in order to set a key for each map item. Does anyone have a quick effecient solution for this case?

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

>Solution :

Just use <React.Fragment key={...}> instead of <>. it’s the same. Ref: https://reactjs.org/docs/fragments.html#keyed-fragments

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