The map( ) Function
map()
map()
is used for iterating through or displaying lists of similar objects of a component in React.
The map()
function creates a new array, which it renders to the DOM. This allows you to manipulate data from an array without changing the original array. It is similar to any of the loops you learned in the JavaScript portion of this book.
To use the map()
function, you chain it to the array you want to iterate over. You will need to pass it a parameter for callback. This callback will be returned as JSX.
Let’s look at an example.
In this example, we are using an array of strings that name the days of the week.
We will chain the map()
function to props
in this example. The callback parameter is day
. ListOfDays
will return day
as a list item. ListOfDays
is contained within the DaysOfTheWeek
function. The DaysOfTheWeek
function is returning an unordered list of ListOfDays
. Notice in this return statement, the array weekDays
is the parameter.
|
|
Keys
Keys and Arrays
The example above used a simple array that contained a collection of strings. The map()
function always uses key-value pairs
. When working with an array, unless otherwise specified, the key becomes the index value of the array element. You can set the key equal to the index like we did in line 13 of the example above, however, this may cause an issue if items are rearranged or added to the array.
Keys and Objects
We can use map()
for more complicated arrays, such as an array of objects. Like the for
loop, you can nest the map()
function.
With all of the flexibility of the map()
function, we need to ensure it renders the correct element.
The key becomes a value’s unique id that React associates with its component. This helps prevent values being mixed up between components. This unique id is crucial if the items in your array can move, be inserted into other components, or even deleted.
It is not advised to create keys while the application is running. You should include them in your data somehow. A key needs to be a string or number data type. If you have the opportunity to create your data, include some sort of id
key. If your data does not have an id
then use what is available.
Let’s look at some data.
We created a calendar.json
file to store our data in our app.
|
|
We have a collection of months
and seasons
. We don’t have any id
values declared in the JSON file. Keys need to be either a number or string. Let’s use the month
values as our keys.
|
|
|
|
If the data had id
, we could use that. In the example below, we updated the calendar
so that each object now contains an id
. The output is the same.
|
|
Mapping conditions
We can also use map()
to render array elements based on conditions. This can be useful if you do not want every item in your list rendered. You can use any of the conditional methods addressed above.
Let’s explore how to use map and conditionals to selectively render items to the UI.
This example uses the same JSON file as the previous example. You can explore it more in “Try It” link below.
You can code along here: Try It!
|
|
In the example above, we import the calendar
and pass it to map()
. The map()
function compares the month
string to the monthChoice
variable. If they are equal, then the <h2>
message will render to the screen. Otherwise, the screen will stay blank. This example uses a ternary operator.
Check Your Understanding
When the code below runs, it produces this warning:
Warning: Each child in a list should have a unique "key" prop.
|
|
How best can we correct our code?
export default ListofNums(key={index});
digits(key={index}).map
<li key={index}>{number}</li>;
This will never be resolved when working with arrays.