I’m trying to create react movie app for my hw and I’m following a tutorial.(https://www.youtube.com/watch?v=jc9_Bqzy2YQ) I’m trying to make the movie display in a row so that I can scroll through them.
return (
<div className='container movie-app'>
<div className='row'>
<div class="col-sm">
<MovieList movies={movies} />
</div>
</div>
</div>
);
}
function MovieList(props) {
return (
<div>
{props.movies.map((movie,index) => (<div>
<img src={movie.Poster} alt="movie"></img>
</div>) )}
</div>
)
}
I’m getting an error code of
react-jsx-dev-runtime.development.js:79 Warning: Each child in a list should have a unique "key" prop.
I tried to fix the key error by assigning it key={imdbID} in movielist.js but that did nothing. I tried changing the classes of the divs to see if that would help but that also did nothing.
3
Answers
If I understand you correctly, for the row, the problem is with your
CSS
. You can style thediv
inside themap
function usingCSS
and assigninline-block
value to thedisplay
property, there are other solutions as well.For the
key
issue, maybe you added it to theimg
element, not thediv
(inside themap
) and eachkey
should be unique.This should fix your problems:
And please make sure that you didn’t add any global
div
styles that can overrideinline-block
.Your code should look like this :
You just need to add
key={index}
or whatever you want, likeid
if you have amove.id
For the display in column, it’s because
div
is adisplay: block
by default, if you want to be a row you need to style yourdiv
withdisplay: inline-block
or you can also style the parent with
flex
etc…Try this, it would fix the problem,