javascript - How to read information of an array in reactjs? - Stack Overflow

I want to loop an array and create list items out of it. In the console,it is showing mistake is thrown

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>;
}
});

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>;
}
});
Share Improve this question edited Mar 8, 2016 at 13:25 Ved 12.1k5 gold badges45 silver badges61 bronze badges asked Mar 8, 2016 at 6:47 user1477955user1477955 1,6708 gold badges24 silver badges37 bronze badges 1
  • 2 keys are required in your <li>s and they can be anything but should be unique for each child. stackoverflow./a/28329550/1642219 – Udit Bhardwaj Commented Mar 8, 2016 at 7:24
Add a ment  | 

2 Answers 2

Reset to default 9

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> 

Just to clarify because i see u use:

  var TodoList = React.createClass({

instead of

class TodoList extends React.Component {

and the question about the closing brackets in the ments above: "Is there a missing closing brackets ((filterItem,index)""

i assume you are not using the es6 syntax, so i wanted to point out that

{ this.filterOptions.map(function(filterItem, index) {
    return (
      <li key={index}>
          <span>{filterItem}</span>
       </li>
    )
  }, this)
}

equals

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743778426a4505659.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信