React Error unique "key" prop. Although key is unique

565 Views Asked by At

I am going to take a list of movies and show them. Each movie has a unique value called id, and I want to use it as a key.
But An error occurs : index.js:1 Warning: Each child in a list should have a unique "key" prop.

--- Skip ---

const Movie = ({
  movies,
  loading,
  nameSearch,
  setNameSearch,
}: MovieProps) => {
  let filterdMovieList: MovieType[] = [...movies];
  if (nameSearch) {
    filterdMovieList.sort();
    filterdMovieList = filterdMovieList.filter((movie: MovieType) => {
      return (
        movie.title.toLowerCase().indexOf(nameSearch) > -1 ||
        movie.title.toUpperCase().indexOf(nameSearch) > -1
      );
    });
  }
  return (
    <div className="movie">
      <MovieSearch setNameSearch={setNameSearch} nameSearch={nameSearch} />
      <div className="movie-container">
        {filterdMovieList.map((movie: MovieType) => {
          {
            console.log(movie.id);
          }
          return <MovieList movie={movie} key={movie.id.toString()} />;
        })}
        {loading && <MovieLoading />}
      </div>
    </div>
  );
};

export default Movie;

I don't understand this error.
And When I logged, the result likes this. enter image description here

It is rendered twice because I changed the state in the parent component's useEffect.

1

There are 1 best solutions below

0
On BEST ANSWER

Your usage of Key in that map looks ok.

The error you have screenshotted says it is in another component, MovieList, that has Fragments without unique keys. Go take a look at that file instead.