Rendering Lists
Rendering an array of objects is a very common pattern.
Consider this array of dogs
and this Component to render a single dog.
You can manually render each component like so
or you can loop/map over the goodest
array and render as many Doggo
components as needed.
JSX knows how to render an array so you can just output DoggoList
(an array of Doggo
components) by putting the variable name in between curly braces:
Alternatively (and more commonly), you could render the result of the .map
operation inline
The key
Prop
key
PropIf you open the Dev Tools console, you will find a warning
When you render an array of components, React expects each component in that array to have a key
prop so that it can keep track of each component internally.
Add a key
prop to each Doggo
you want to render. Most datasets will have a unique id for each item, goodest
doesn't so just use the name property instead.
You should now see key
s for each Doggo
component in the React Dev Tools
Last updated
Was this helpful?