反应状态数组添加值

时间:2016-04-05 05:28:04

标签: javascript reactjs

我确定这是显而易见的事情然后我会感到愚蠢,但我在这里错过了什么?我有类似的东西:

export default class Questions extends Component {

  state = {
    questions: [{value: 'one'}, {value: 'two'}]
  };

  addQuestion = (question) => {
    let questions= this.state.questions
    let newQuestions = [...questions, {value: question}]
    this.setState({
      questions: newQuestions
    })
  };

  render() {
    return (
      <div>
        {this.state.questions.map((question, index) => <p key={index}>{question.value}</p>)}
        <button onClick={this.addQuestion.bind('three')}>Add</button>
      </div>
    )
  }

}

初始状态值显示正常,但是当我点击添加时,我从React得到一个很长的错误:

  

未捕获错误:不变违规:对象无效作为React   孩子......

那么,我在这里错过了什么?

1 个答案:

答案 0 :(得分:2)

最有可能是:

{this.addQuestion.bind(this, 'three')}

否则,您将无法访问this.state中的addQuestion

let questions= this.state.questions