How to display an array in ReactJS
Introduction to ReactJS
ReactJS is a popular open-source JavaScript library used for building user interfaces, particularly for single-page applications. It's used for handling the view layer in web and mobile apps. ReactJS allows us to create reusable UI components. In this blog post, we will learn how to display an array in ReactJS.
Understanding Arrays in JavaScript
Before we get into ReactJS, let’s refresh on what an array is. In programming, an array can be likened to a list in real life. Just like a shopping list that contains a series of items, an array is a collection of items. These items could be numbers, strings, objects, or even other arrays.
Let's take a look at a simple array in JavaScript:
let groceryList = ["Apples", "Oranges", "Bananas"];
In this example, groceryList
is an array that contains three strings.
The .map() Function in JavaScript
JavaScript provides a function called .map() that is used to transform the elements in an array. Think of it like a factory conveyor belt. You have your raw materials (the original array elements) going in, they pass through the factory (the .map() function), and come out as a different product (the new array elements).
Here's how you would use the .map() function to add "!" to every item in our groceryList array:
let excitedGroceryList = groceryList.map(item => item + "!");
Now, excitedGroceryList
is an array that contains: "Apples!", "Oranges!", "Bananas!".
Displaying an Array in ReactJS
Now let's dive into how to display this array in React.
React uses a syntax extension called JSX that allows you to write HTML in your JavaScript. This might seem a bit weird at first, but it actually makes a lot of sense since in React, your HTML is often closely tied to your JavaScript.
To display an array in React, we can use the JavaScript map() function inside our JSX. Let's create a React component that displays our grocery list:
import React from 'react';
function GroceryList() {
let groceryList = ["Apples", "Oranges", "Bananas"];
return (
<ul>
{groceryList.map(item => <li>{item}</li>)}
</ul>
);
}
export default GroceryList;
In this code, we're creating a GroceryList
component. Inside the component, we define our groceryList
array. In the return statement, we use JSX to create an unordered list (
Inside the curly braces, we're using the .map() function to transform each item in the groceryList into a list item (
- ). The result is that each item in the array is output to the browser as a list item in an unordered list.
Key Prop in React
If you would run the above example, it would work but React would complain with a warning in the console. It would say something about a missing "key" prop.
The "key" prop is a special string attribute you need to include when creating lists of elements in React. Keys help React identify which items have changed, are added, or are removed. Keys should be given to the elements inside the array to give the elements a stable identity.
Let's add keys to our grocery list items:
import React from 'react';
function GroceryList() {
let groceryList = ["Apples", "Oranges", "Bananas"];
return (
<ul>
{groceryList.map((item, index) => <li key={index}>{item}</li>)}
</ul>
);
}
export default GroceryList;
In this updated example, we're passing a second argument to the function we give to .map(). This second argument is the index of the current item in the original array. We're then using this index as a key for each list item.
You might be wondering why we don't just use the grocery item as the key, like "Apples" or "Bananas". The reason is that keys need to be unique. If we had two "Apples" in our list, there would be a conflict.
Conclusion
Displaying arrays in React doesn't have to be complicated. With a basic understanding of JavaScript arrays and the .map() function, you can easily display arrays in your React applications.
Remember, React is just JavaScript. The more comfortable you become with JavaScript, the easier it will be to understand and use React. Happy coding!