Sunday 24 January 2016

javascript - How to write for loop in JSX in React?




I want to write a simple program where using for loop it will print numbers from 0 to 10. I am trying to use a for loop which will print numbers from 0 to 10 and will pass the props to child component. Here is my code:



import React, { Component } from 'react';
class App extends Component {




render() {

return(


{
for(var i=0;i<11;i++)
{
// var data=i;


}
}



);
}
}

const Print=(props)=>{
return(


{props.value}

);
}

export default App;

Answer



You can push the JSX to an array and render that.






class App extends React.Component {
render() {
const result = [];

for (var i = 0; i < 11; i++) {
result.push();
}


return
{result}
;
}
}

const Print = props => {
return
{props.value}
;
};

ReactDOM.render(, document.getElementById("root"));









No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...