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

React error: "Each child in a list should have a unique "key" prop."

Hi I am getting an error that says "each child in a list should have a unique "key" prop. When I check the Components using React Developer Tools I don’t see any duplicates – what am I doing wrong?

  return (
    <>
    <Modal
        isOpen={modalIsOpen}
        onRequestClose={closeModal}
    >
      { modalIsOpen ? (
        <Note
        key={'ModalNote' + modalNote.id}
        id={modalNote.id}
        title={modalNote.title}
        text={modalNote.text}
        date={modalNote.date}
        deleteNote={deleteNote}
        closeNote={closeModal}
        />
      ) : ('')
      }
      
    </Modal>
    <div className="notesForm">
      <AddNote addNoteHandler={addNoteHandler}/>
    </div>
    <div className="notes">
      {notes.map((note) => (
        <>
        <Note
          key={note.id}
          id={note.id}
          title={note.title}
          text={note.text}
          date={note.date}
          deleteNote={deleteNote}
          closeNote={closeModal}
          openModal={openModal}
          modalIsOpen={modalIsOpen}
        />
        </>
      ))}
    </div>
    </>
  );

>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

The keys are to be added to the outermost element returned from the map

Here in your e.g its the fragment <>, to add the key to the fragment you should be using <React.Fragment> for shorthand as shorter syntax doesn’t support keys

{notes.map((note) => (
        <React.Fragment key={note.id}>
        <Note
          key={note.id}
          id={note.id}
          title={note.title}
          text={note.text}
          date={note.date}
          deleteNote={deleteNote}
          closeNote={closeModal}
          openModal={openModal}
          modalIsOpen={modalIsOpen}
        />
        </React.Fragment>
      ))}
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