0
所以我試圖將狀態傳遞給一個問題組件。由於某種原因,國家並沒有被傳染。我敢肯定,這件事顯而易見,但我可以在這一件事上使用另一套眼睛。謝謝!道具沒有傳遞給子組件
import React from 'react';
import Question from './Question';
import firebase from 'firebase';
var questions = [{ qtext : "", options: [], id:0, answer: ""}, { qtext : "", options: [], id:1, answer: "" }];
const QuizBuilderForm = React.createClass({
getInitialState: function() {
return {
questions
};
},
addQuestion: function(questions, id) {
questions = this.state.questions;
questions.push({ qtext : "", options: [], id: this.state.questions.length });
this.setState({
questions: questions
});
},
handleSubmit: function(event) {
event.preventDefault();
console.log(this.state.questions);
this.firebaseRef = firebase.database().ref('quizzes');
this.firebaseRef.push({
question: this.state.questions
});
this.refs.form.reset();
this.setState({
question: [{ qtext : "", options:[], id: 0, answer: ""}, {qtext: "", options:[], id: 1, answer: ""}]
});
},
render: function() {
{this.state.questions.map((question, index) => {
<Question {...this.props} key={index} index={index} question={question} />
console.log(question);
}
)};
return (
<form className="quiz-form" onSubmit={this.handleSubmit} ref="form">
<Question />
<button type="button" className="add-question" onClick= {this.addQuestion} disabled={this.state.questions.length === 5}>{this.state.questions.length < 5 ? 'Add another question' : 'Question limit reached!'}</button>
<button type="submit">Create Quiz</button>
</form>
);
}
});
export default QuizBuilderForm;
謝謝!對於您的渲染方法,我會收到意外令牌的錯誤。它抱怨第一個'。'在'{this.state.questons.map ....}'中。 – maxwellgover
'var questions = this.state.questions.map((question,index)=> {Question {... this.props} key = {index} index = {index} question = {question} /> console.log(question); });' 只需要刪除包裹整個函數 – finalfreq
@finalfreq哇的塊。好。非常感謝。 – maxwellgover