Table of Contents
In this tutorial, we will learn how to write loops inside JSX in React.
Setting up the project
Let's create a new React app using the following command:
1npx create-react-app react-for-loop
Let's add some styling to the page in index.css
:
1body {2 margin: 10px auto;3}4.app {5 margin: 0;6 position: absolute;7 top: 0;8 left: 50%;9 transform: translate(-50%, 0%);10}
Using the map function
In our application, let's display a list pizza toppings:
1const toppings = [2 "Golden Corn",3 "Paneer",4 "Tomato",5 "Mushroom",6 "Onion",7 "Black Olives",8]9function App() {10 return (11 <div className="app">12 <h2>Pizza Toppings</h2>13 <ul>14 {toppings.map(topping => {15 return <li>{topping}</li>16 })}17 </ul>18 </div>19 )20}2122export default App
In the above code,
- We have declared a list of Pizza toppings (as you may have guessed, I am a vegetarian and can't even imagine pineapple as a topping 🤮)
- Inside the render function, we are using the JavaScript map function for looping the list.
- The map function accepts a callback, which receives the current item as the first argument (the name of the topping in our case). Then, we return the JSX that need to be rendered for each topping (a list item in our case).
Adding key to the list
If you start the application, run it in the browser, and open the browser console, you will see a warning as shown below:
React is warning us that each item in the list should have a unique key.
Let's understand why keys are important: Say, for example, one of the items in the list changes, then having keys help React in identifying which item has changed in order to re-render the list. You can read more about keys here.
1const toppings = [2 "Golden Corn",3 "Paneer",4 "Tomato",5 "Mushroom",6 "Onion",7 "Black Olives",8]9function App() {10 return (11 <div className="app">12 <h2>Pizza Toppings</h2>13 <ul>14 {toppings.map(topping => {15 return <li key={topping}>{topping}</li>16 })}17 </ul>18 </div>19 )20}2122export default App
Since the name of the topping is unique, we have used it as the key. If there aren't any unique fields you may use the index of the array as well, as shown below:
1toppings.map((topping, index) => {2 return <li key={index}>{topping}</li>3})
Implicit returns
Since we are using an arrow function as a callback function, we can write implicit returns since there is only a single line of code inside the callback.
1const toppings = [2 "Golden Corn",3 "Paneer",4 "Tomato",5 "Mushroom",6 "Onion",7 "Black Olives",8]9function App() {10 return (11 <div className="app">12 <h2>Pizza Toppings</h2>13 <ul>14 {toppings.map(topping => (15 <li>{topping}</li>16 ))}17 </ul>18 </div>19 )20}2122export default App
Separating the Logic and the JSX
If you are one of those persons who doesn't like to mix logic and JSX (surprisingly, I am not one of them! I like to mix logic and JSC no matter how clumsy it gets.), you could have a separate function to have an array populated with the list of JSX as shown below.
1const toppings = [2 "Golden Corn",3 "Paneer",4 "Tomato",5 "Mushroom",6 "Onion",7 "Black Olives",8]910let listToRender = []1112const generateList = () => {13 for (let index = 0; index < toppings.length; index++) {14 const topping = toppings[index]15 listToRender.push(<li key={index}>{topping}</li>)16 }17}1819generateList()2021function App() {22 return (23 <div className="app">24 <h2>Pizza Toppings</h2>25 <ul>{listToRender}</ul>26 </div>27 )28}2930export default App
Source code
You can view the source code here and do let me know in the comment section below about your favorite style of looping in React.
Do follow me on twitter where I post developer insights more often!
Leave a Comment