JavaScript FrameworksReact

Rendering Lists in React

Rendering lists in React can be a little tricky, but it works just like vanilla JavaScript. When rendering a list directly in the template, you must use the map() function as it returns a view. The forEach() works similarly except it doesn’t return data so it won’t do you any good. However, you can use all types of loops outside of the template to build a list of components and render the list object to the view. Below are some examples.

JSX

Alternatively, you can build the view first using the map() function and just render the object in the template like the following.

JSX

This time let’s use the forEach() loop.

JSX

Finally, let’s just use the for loop, which also works similarly with the while loop.

JSX

Either way, so long as you can build the components you can then just use it in the template and let React render it.

Verified by MonsterInsights