我确定这是显而易见的事情然后我会感到愚蠢,但我在这里错过了什么?我有类似的东西:
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 孩子......
那么,我在这里错过了什么?
答案 0 :(得分:2)
最有可能是:
{this.addQuestion.bind(this, 'three')}
否则,您将无法访问this.state
中的addQuestion
:
let questions= this.state.questions