我将列出电影清单并展示它们。每部电影都有一个唯一的值,称为 id,我想用它作为键。
但是会发生错误:index.js:1 警告:列表中的每个孩子都应该有一个唯一的“key”道具。
--- 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;
它被渲染了两次,因为我更改了父组件的 useEffect 中的状态。