Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript React状态数组添加值_Javascript_Reactjs - Fatal编程技术网

Javascript React状态数组添加值

Javascript React状态数组添加值,javascript,reactjs,Javascript,Reactjs,我相信这是显而易见的,然后我会觉得很傻,但我在这里错过了什么?我有点像: export default class Questions extends Component { state = { questions: [{value: 'one'}, {value: 'two'}] }; addQuestion = (question) => { let questions= this.state.questions let newQuestions

我相信这是显而易见的,然后我会觉得很傻,但我在这里错过了什么?我有点像:

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

}
导出默认类问题扩展组件{
状态={
问题:[{value:'one'},{value:'two'}]
};
addQuestion=(问题)=>{
让问题=this.state.questions
让newQuestions=[…questions,{value:question}]
这是我的国家({
问题:新问题
})
};
render(){
返回(
{this.state.questions.map((问题,索引)=>

{question.value}

)} 添加 ) } }
初始状态值显示良好,但当我单击“添加”时,我从React about得到一个很长的错误:

未捕获错误:不变冲突:对象作为对象无效 孩子


那么,我错过了什么

最有可能是:

{this.addQuestion.bind(this, 'three')}
否则,您将无法在
addQuestion
中访问
this.state

let questions= this.state.questions

是的,就是这样。仍然习惯于ES6 arrow函数提供的词法
this
绑定,其中
this
是或不是必需的。谢谢你,先生!