user1477955 user1477955 - 4 months ago 17
Javascript Question

How to read information of an array in reactjs?

I want to loop an array and create list items out of it. In the console,it is showing mistake is thrown, because my array has no keys but only values. So What is the right operation to read out an array?

*// this.props.items = ["cars","streets","houses"];*Wrong. You can't update props

var TodoList = React.createClass({
render: function() {
var createItem = function(item) {
return <li>{item}</li>;
};
return <ul>{this.props.items.map(createItem)}</ul>;
}
});

Ved Ved
Answer Source

Try this way:

this.filterOptions =['Monthly','Weekly','Daily'];

   <ul>
       {  this.filterOptions.map((filterItem) => {
       return (
                <li key={filterItem}>
                    <span>{filterItem}</span>
                 </li>
              );
          })
       }
     </ul>

EDIT 1: If there is duplicate value in array,

   <ul>
       {  this.filterOptions.map((filterItem,index) => {
       return (
                <li key={index}>//key must be uniq
                    <span>{filterItem}</span>
                 </li>
              );
          })
       }
     </ul>