javascript - this.state.items.map is not a function - Stack Overflow

I do console.log(items) I got ['a','b','c'] but I got error of map is not

I do console.log(items) I got ['a','b','c'] but I got error of map is not a function.

..    
    var Todo_list = React.createClass({
      getInitialState(){
        return { items:['a','b']}
      },
      addItem(item){
      this.setState({items:this.state.items.push(item)})
        console.log(this.state.items) // this is working
      },
      render() {
        return (
        <div>
         <TodoInput addItem={this.addItem} />

         {this.state.items.map((item,i) =>  <li key={i}>{item}</li> )}
         </div>
        );
      }
    });
..

/ Try to add an item, I wonder why, I guess I've pushed it correctly?

I do console.log(items) I got ['a','b','c'] but I got error of map is not a function.

..    
    var Todo_list = React.createClass({
      getInitialState(){
        return { items:['a','b']}
      },
      addItem(item){
      this.setState({items:this.state.items.push(item)})
        console.log(this.state.items) // this is working
      },
      render() {
        return (
        <div>
         <TodoInput addItem={this.addItem} />

         {this.state.items.map((item,i) =>  <li key={i}>{item}</li> )}
         </div>
        );
      }
    });
..

https://jsfiddle/tehwajh2/ Try to add an item, I wonder why, I guess I've pushed it correctly?

Share Improve this question asked Oct 4, 2016 at 14:59 Thian Kian PhinThian Kian Phin 9313 gold badges13 silver badges27 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

In this case you can use .concat instead of .push, because .push returns the new length of the array., length is Number, and Number does not have .map method that's why you get error

 this.setState({ items: this.state.items.concat(item) })

Example

The push() method adds one or more elements to the end of an array and returns the new length of the array.

The concat() method returns a new array prised of the array on which it is called joined with the array(s) and/or value(s) provided as arguments.

You can try adding this.state.items.push(item); seperately. It works

var TodoInput = React.createClass({
    handleAddItem(){
        var todo_val = this.refs.todo_val.value;
      this.props.addItem(todo_val);
  },
  render() {
    return (
    <div>
     <input ref='todo_val' type="text" />
     <button onClick={this.handleAddItem}>Add</button>
     </div>
    );
  }
});

var Todo_list = React.createClass({
  getInitialState(){
    return { items:['a','b']}
  },
  addItem(item){
  console.log(item);
 this.state.items.push(item);
  this.setState({items: this.state.items});
    console.log(this.state.items) 
  },
  render() {
    return (
    <div>
     <TodoInput addItem={this.addItem} />
     {this.state.items.map(function(item, key) {
        return (
        <li key={key}> {item}</li>
      )

     })}
     </div>
    );
  }
});

ReactDOM.render(
  <div>
  <Todo_list />
  </div>,
  document.getElementById('container')
);

JSFIDDLE

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

相关推荐

  • javascript - this.state.items.map is not a function - Stack Overflow

    I do console.log(items) I got ['a','b','c'] but I got error of map is not

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信