简体   繁体   中英

warning like : Each child in a list should have a unique "key" prop

import React, { useContext , useEffect } from 'react'
import noteContext from '../context/notes/noteContext';
import AddNote from './AddNote';
import NoteItem from './NoteItem';


const Notes = () => {
  const context = useContext(noteContext);
  const { notes, getNotes } = context;
  useEffect(() => {
    getNotes()
  
  }, [])
  
  return (<>
    <AddNote />
    <div className=" row my-3 mx-3">
      <h3>YOUR NOTE</h3>
      {notes.map((note) => {
        return <NoteItem key={note._id} note={note} />
      })}
    </div>
  </>
  )
}


export default Notes;

This is the code where i am rendering and using id as key


    // API CALL
    const response = await fetch(`${host}/api/notes/fetchallnotes`, {
      method: 'GET',
      headers: {
        'Content-Type': 'application/json',
        "auth-token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjp7ImlkIjoiNjJiNDRmMjIzNWVhZmNkM2FhODRmMTg1In0sImlhdCI6MTY1NTk4NzgyNH0.LRueOf_bDWJB6NJ5jmN-ZQxStPPUt0ppW2G0s5VcRr4"
      },
    });
    const json = await response.json()
    console.log(json);
    setNotes(json);

  }

and here i am fetching the notes it is the not the first time that i am having this problem this is my whole console error react_devtools_backend.js:4026 Warning: Each child in a list should have a unique "key" prop.

Check the render method of Notes . See https://reactjs.org/link/warning-keys for more information. at NoteItem (http://localhost:3000/static/js/bundle.js:781:68) at Notes (http://localhost:3000/static/js/bundle.js:923:68) at div at Home at Routes (http://localhost:3000/static/js/bundle.js:40285:5) at div at Router (http://localhost:3000/static/js/bundle.js:40218:15) at BrowserRouter (http://localhost:3000/static/js/bundle.js:39693:5) at NoteState (http://localhost:3000/static/js/bundle.js:1033:76) at App

can anyone please tell actually where i am getting error and which file i should share so that you can help me or where i should work on to remove this error

it is showing me error please do help me

could you please use index instead of note._id , because note._id may be duplicates, you are receiving a warning, so you can use index instead of note._id .

{notes.map((note, index) => {
  return <NoteItem key={index} note={note} />
})}

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