
In code, youāll often find that different lists of data are structured in the same way. Great news! 𤩠There's no need to strain over copying and pasting: you can just loop through your content and generate React components from it.Ā Ā
But what would that look like?
The JavaScript Ā map()Ā method passes over each element of an array. It applies a function and returns a new array containing the results of this function applied to each element.Ā
For example, for a function that doubles the value of an element, it would look like this:
const numbers = [1, 2, 3, 4]
const doubles = numbers.map(x => x * 2) // [2, 4, 6, 8]In our case,Ā you can use it to transform a list of dataĀ into a list of components.Ā Ā
Ā Letās create a Ā ShoppingList.jsĀ component for our plant shop.
In this file, declare a Ā plantListĀ variable, which containsĀ a list of plants:Ā
const plantList = [
'Monstera',
'Ficus Lyrata',
'Silver Pothos',
'Yucca',
'Palm'
]Below that, add the component itself:Ā
function ShoppingList() {
return (
<ul>
{plantList.map((plant) => (
<li>{plant}</li>
))}
</ul>
)
}
export default ShoppingListAs you can see, for each entry in the array, it returns a Ā <li>Ā element.Ā
Now import ShoppingList.js into Ā <App />Ā Ā and⦠fantastic! We have our list of components. š„³
But whatās this big red error on my console?

Uh oh! Looks like the Ā keyĀ Ā prop is missing. š
Right, so what is theĀ Ā keyĀ prop, and why do we need to worry about it here?
Ā React's documentation clearly explains this:Ā keyĀ props help ReactĀ identify which items have changed, are added, or are removed. Keys should be given to the elements inside the array toĀ provide a stable identity.Ā Ā
So I can give my key prop any value I like?Ā Ā
Not quite! To avoid bugs, a key must follow these two rules:
It should be unique within the array.
It should be stable over time (the same source data should always have the same key= value).
Remember the virtual DOM in Part 1? The Ā keyĀ is used toĀ link a piece of data to the corresponding component in the virtual DOM,Ā allowing for components to be generated. Ā
Ā There are various methods for generating unique Ā keyĀ props:
The easiest and most reliable method is using the ID from data in your database.
You could also find a way to use the data valueĀ if youāre sure it will always be unique and stable over time.
As a last resort, you could define a string and combine it with the item index.Ā
Since we donāt have an ID, weāll combine the index and the plant name, which is a string:
function ShoppingList() {
return (
<ul>
{plantList.map((plant, index) => (
<li key={`${plant}-${index}`}>{plant}</li>
))}
</ul>
)
}
export default ShoppingListAndĀ it's error free!
On branch P2C3-Begin-1, youāll find the codebase you need to start the exercise. In Ā ShoppingList.jsĀ ,Ā youāll see a list of plants imported from Ā /datasĀ , called Ā plantListĀ ,Ā and which consists of categories for each plant.Ā Ā
Try the following, using this list of plants:
Dynamically put together a new list that extracts the unique plant categories (which weāll use later on).
There are various ways of creating a list of unique items but use theĀ reduce()Ā orĀ forEach()Ā method.
Display the list of plant categories above the list of plants for sale.Ā Ā
The solution is on branch P2C3-Solution-1.Ā
React allows you to create lists of components, saving developers vast amounts of time. But thatās not all! You can also use the JSX to render elements in components conditionally.Ā
Remember that React allows you to evaluate a JavaScript expression in the JSX directly? That you can use curly braces to write a conditional to display one element or another?
Let'sĀ take this concept further, changing certain parts of our component depending on the context.
You've already encountered the conditional operator. Now that we have a complete app to play around with, let's put it to work in our plant shop. š¤Ā
In our list of plants Ā plantList.jsĀ , add an isBestSaleĀ category that corresponds to a Ā booleanĀ , which tells you whetherĀ a plant isĀ a best seller. It looks like this:Ā
{
name: 'Monstera',
category: 'Classic',
id: '1ed',
isBestSale: true
},Ā Now that we have our boolean,Ā let'sĀ use a conditional to display different emojis according to whether it is a best seller or not. In Ā ShoppingList.jsĀ , on the same level as the name, add:
{plantList.map((plant) => (
<li key={plant.id}>
{plant.isBestSale ? <span>š„</span> : <span>š</span>}
</li>
))}Which gives us:

Great! It works!
On second thought, it might not work as a sales argument. š¤Ā Instead, letās just put š„Ā for the best sellers and nothing for the others.
Ā To do that,Ā return Ā nullĀ in the condition where you donāt want anything to be displayed:
{plant.isBestSale ? <span>š„</span> : null}It works! š¤©
Do you know what, though? Thereās an even easier way to write this ā you can use Ā &&Ā .Ā
Ā Placed between curly braces,&&Ā goes before a JSX element and states that the element will only be generated if the condition is met. SoĀ you could write:
{plant.isBestSale && <span>š„</span>}Which gives us:

Yay! It works!
React is very flexible, and there are other methods you can use to add context to your content. Iāll run through a few of these in the screencast below:
Youāll find the codebase on branch P2C3-Begin-2. There's some CSS in Ā ShoppingList.cssĀ so that everythingās a bit clearer. āØĀ
There's also aĀ Ā isSpecialOfferĀ variable to Ā trueĀ for plants on sale. Your mission is toĀ render a Ā divĀ element that contains the word "Sale"Ā depending on whether the plant is on offer. You can pass it the Ā jh-salesĀ class to add style.Ā
Feel free to choose any of the methods from this chapter to create your conditional rendering.
Youāll find the solution on branch P2C3-Solution-2.
Using a list of data, the Ā map()Ā method creates a list of React components.
The Ā keyĀ prop is essential in lists of components.Ā
To avoid bugs, your Ā keyĀ prop should:Ā
Be unique within the list.
Remain the same over time.
Best practice for creating a Ā keyĀ is using the dataās unique Ā idĀ , and not using the itemās index.Ā
Conditional rendering is used to render one element or another in the JSX, according to the condition āif⦠else...ā.
There are other ways of creating conditions in React, such as taking the conditions out of the JSX.Ā
In this chapter, youāve mastered two extremely useful ways of saving time in React with conditions and lists. Congratulations!Ā
In the next chapter,Ā we willĀ look at an essential element for reusing components in React: props! Canāt wait to get PROPerly acquainted? Then see you in the next chapter!Ā