Different ways to map a list in React JS
There are several ways to map a list in React JS. Here are some most popular ways that the majority of developers using these days.
Some of the most common ones are:
1. Using the Array.map() method:
This method creates a new array with the results of calling a provided function on every element in the original array. In React, you can use this method to render a list of items as a set of React components.
For example:
const myList = ['item1', 'item2', 'item3'];
const myComponentList = myList.map((item, index) => (
<li key={index}>{item}</li>
));
return (
<ul>
{myComponentList}
</ul>
);
2. Using the for...of loop:
This loop allows you to iterate over the values of an iterable object, such as an array. In React, you can use this loop to render a list of items as a set of React components.
For example:
const myList = ['item1', 'item2', 'item3'];
const myComponentList = [];
for (const item of myList) {
myComponentList.push(<li>{item}</li>);
}
return (
<ul>
{myComponentList}
</ul>
);
As you can see in the above snippets, outside of the return statement we are binding objects to a newly created array and then using that array in the return block.
3.Using the Array.forEach() method:
This method allows you to execute a provided function once for each array element. In React, you can use this method to create an array of React components and then render them.
For example:
const myList = ['item1', 'item2', 'item3'];
const myComponentList = [];
myList.forEach((item, index) => {
myComponentList.push(<li key={index}>{item}</li>);
});
return (
<ul>
{myComponentList}
</ul>
);
It's way handier than for..of loop.
This method does the same thing as we did through the for..of loop but as we know Foreach loop in Javascript has its extension methods so it will be a lot easier to use them quickly where needed instead of creating new loops and variables.
Conclusion:
Overall, the choice of method depends on the specific requirements of your React application and personal preferences. I will prefer you to use the best possible way that enhances your application performance and that is the first method (using map funtion) if there is no other criteria to acheive.
As we all know, developers using React are more tend to improve the performance of the application rather than adding pushing code into the app without knowing how harmful it is, from the performance point of view.
Most beginners make these mistakes when they just need to finish the features and make the app just working.
Support Me
Join me on Codementor for more helpful tips. Make sure to like and Follow to stay in the loop with my latest articles on different topics including programming tips & tricks, tools, Framework, Latest Technologies updates.
Please support me on PATREON on below link.
Support me on Patreon
Thank you very much for supporting me.
I would love to see you in the followers list on code mentor.
Stay tuned and stay updated!
This code was explained in very easy method.
Thank you Preeti for appreciating.
Follow me on Patreon