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.

It is rendered twice because I changed the state in the parent component's useEffect.
Your usage of Key in that map looks ok.
The error you have screenshotted says it is in another component,
MovieList, that hasFragments without unique keys. Go take a look at that file instead.