63

I'm building an app using the google books API and I appear to be passing a unique key to each child in the list, but the error won't go away. I must be doing something wrong but I'm not sure what.

const BookList = (props) => {

//map over all of the book items to create a new card for each one in 
the list
    const books = props.books.data.items.map((book) => { 
        console.log(book.id)
        return (
            <div className="col col-lg-4 grid-wrapper">
                <BookCard 
                    key={book.id}
                    image={book.volumeInfo.imageLinks.thumbnail}
                    title={book.volumeInfo.title} 
                    author={book.volumeInfo.authors[0]} 
                    description={book.volumeInfo.description}
                    previewLink={book.volumeInfo.previewLink}
                    buyLink={book.saleInfo.buyLink}
                />
            </div>
        ); 
    })

    return (
        <div>{books}</div>
    );
}

Notice that after the return in const books I have a console.log(book.id), which will display all 10 unique id keys in the console. But when I try to pass it to the child of this component using key={book.id}, I get this error.

Matt Brody
  • 1,083
  • 3
  • 10
  • 20

1 Answers1

109

The key needs to go on the outermost returned element. In your specific case, that means changing this:

            <div className="col col-lg-4 grid-wrapper">
                <BookCard 
                    key={book.id}

to this:

            <div className="col col-lg-4 grid-wrapper" key={book.id}>
                <BookCard 
  • 3
    That did it! Is there a specific reason why the key has to be on the outermost returned element? – Matt Brody Mar 14 '19 at 02:28
  • 16
    When React goes through an array, it only looks at the things directly in it. It doesn't recurse all the way through looking for a key. If it did, it would (1) be slow, and (2) cause ambiguity when there were nested arrays. – Joseph Sible-Reinstate Monica Mar 14 '19 at 02:29
  • Brilliant solution – John Nyingi Feb 22 '21 at 10:32
  • 1
    In case this got anyone else: If you define a custom component, and that's what's inside your list, the key has to go on the props of the component itself, NOT the DOM element that the component returns. – Mark P Neyer May 19 '22 at 12:54
  • That works for me as well, I had a parent element below the .map and it continued to complain about that, I was focusing on the nested list for several hours, now I have both of them loading without the error – Dave Haines May 29 '22 at 20:46