Author -  Sai gowtham

How to iterate through arrays/lists in react

In this tutorial, we are going to learn about how to loop/iterate through the arrays in the react.js.

Let’s see some examples.

Suppose we have an array of users and we need to render it in the browser.


function User(props){

   return (
     <li>{props.name}</li>
   )
}


const users = [

    {id:1,name:"local"},
    {id:2,name:"kid"},
    {id:3,name:"king"},

    ]

function Users(){

  return(
      <ul>
       <User name={users[0].name} />
       <User name= {users[1].name}/>
       <User name={users[2].name}/>
      </ul>
  )
}

iterate-through-lists-react

In the above code, we manually passed the each user name by using the index or position.it’s a difficult task to pass each user manually when we have 1000 users or more.

To iterate through the arrays in react we need to use map() method instead of for loops mostly use in angular and vue apps.

If you don’t know about map(), then checkout how to use map method.

Let’s refactor our code by using the map() method.

function User(props){

   return (
       <ul>
       <li>{props.name}</li>
       </ul>
   )
}


const users = [

    {id:1,name:"local"},
    {id:2,name:"kid"},
    {id:3,name:"king"},

    ]

function Users(){

  return(
      <ul>
         {users.map((user)=><User name={user.name}/>)}
      </ul>
  )
}

We used curly braces { } because its a JavaScript expression.The map method loop through the users’ array and render each user in the browser.

if you open your console you will see an Warning. Warning: Each child in an array or iterator should have a unique "key" prop.

array or iterator should have a unique

This warning is due to the key prop is missed in User component.whenever we are iterating through the arrays in react we need to pass the unique key prop because in the dom react needs to keeps track each user by using a key prop.

Let’s add the key prop to our code.

function Users(){

  return(
      <ul>
         {users.map((user)=><User key={user.id} name={user.name}/>)}
      </ul>
  )
}

In our users array, there is an id property which is unique in each user.so that we are passing the id to the “key” prop.

If you open react dev tools you can see a key prop.

react key prop

Css Tutorials & Demos

How rotate an image continuously in CSS

In this demo, we are going to learn about how to rotate an image continuously using the css animations.

How to create a Instagram login Page

In this demo, i will show you how to create a instagram login page using html and css.

How to create a pulse animation in CSS

In this demo, i will show you how to create a pulse animation using css.

Creating a snowfall animation using css and JavaScript

In this demo, i will show you how to create a snow fall animation using css and JavaScript.

Top Udemy Courses

JavaScript - The Complete Guide 2023 (Beginner + Advanced)
JavaScript - The Complete Guide 2023 (Beginner + Advanced)
116,648 students enrolled
52 hours of video content
$14.99 FROM UDEMY
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
631,582 students enrolled
49 hours of video content
$24.99 FROM UDEMY
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
203,937 students enrolled
31.5 hours of video content
$14.99 FROM UDEMY