Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent in React of *ngFor in Angular 2

Tags:

I want to show a dynamic select, with values between 1 and 100 in a react component.

In Angular 2 is

<select>     <option *ngFor="let i of numbers">{{i}}</option> </select>  <!--numbers is an array [1,2,3,4,...100]--> 

How is this in React?

like image 719
Mateo Guzmán Avatar asked Apr 27 '17 19:04

Mateo Guzmán


People also ask

What is equivalent of ngFor in React?

We often need to render an array in the UI. Angular does this by using *ngFor — an HTML construct. React does this by using map() — a JS method. That's about it for rendering arrays in Angular and React.

When to use Angular vs React?

Angular is mostly used to build complex enterprise-grade apps like single-page apps and progressive web apps, while React is used to build UI components in any app with frequently variable data. Angular's learning curve is steeper due to its too many in-built functionalities while React's smaller package size.

Can we use React code in angular?

The supported version of React is 15.3. 2. Server-side rendering is not supported. To be able to use the GoodData React Components in your Angular 2+ environment, wrap each component into an Angular component, and then render the React component using ReactDom.

What is the equivalent of a div in react native?

View is the most common element in React Native. You can consider it as an equivalent of the div element used in web development.


2 Answers

You can use map on numbers and create the options dynamically like this:

<select>     {        numbers.map(el => <option value={el} key={el}> {el} </option>)     } </select> 

Check this example:

var numbers = [...Array(100).keys()];    var App = () => {     return(        <select>          {             numbers.map(el => <option value={el} key={el}> {el} </option>)          }        </select>     )  }    ReactDOM.render(<App/>, document.getElementById('app'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>  <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>    <div id='app'/>
like image 137
Mayank Shukla Avatar answered Sep 22 '22 12:09

Mayank Shukla


RepeatModule is the equivalent in Reactjs

ReactDOM.render(<RepeatModule items={items} />,                      document.getElementById('react-content')); 

LIVE DEMO

like image 28
Aravind Avatar answered Sep 20 '22 12:09

Aravind