I am new to React.js. I am trying to bind data arrays. I am looking for the equivalent of ng-repeat, but i can't find it inside documentation.
e.g:
var data = ['red', 'green', 'blue']
using angular i would have done something like this in my html:
<div ng-repeat="i in data">{{i}}</div>
I am wondering the React's markup to do this
To perform the same task as ng-repeat in React you just need to think natively. Under the hood ng-repeat is just using a native Javascript iterator. You can use the same sort of native iterator directly in React. For example, I`ll use Array.map:
I got the above example from http://angulartoreact.com/ng-repeat-react-equivalent . The site has more examples of React equivaents to Angular directives.
In React, you just write the necessary JavaScript (and potentially build it into a reusable control as you'll see). It's very prescriptive and easy to do once you learn the basic patterns (and how they differ from AngularJS).
So, in the
render
function, you'd need to iterate through the list. In the example below, I've usedmap
, but you could use other iterators as needed.Here it is in use.
And, as you can see, you can quickly build a reusable component that can "repeat" through the list.
Should just be:
In your render function inside a react component you can do this:
And now you can return the dataComponent.
Here is an example using ES6, and a stateless component.
The below code demonstrates creating a menu by looping through a list of menu items.
Basically what we're doing is just pure javascript iteration utilizing Array.map.