我正在尝试使用对Codepen做出反应的单个问题测验应用程序。我正在使用api来获取问题,3个错误答案和对该问题的正确答案,并将其添加到状态中。验证答案时出现问题。单击4个选项之一后,我想有条件地渲染一个div。但是问题在于,在显示结果之后,将呈现另一个单选输入。
const appRoot = document.getElementById('app');
class App extends React.Component{
state={
question:'',
correct:'',
incorrect:[],
result: null
}
componentDidMount(){
axios.get('https://opentdb.com/api.php?amount=1&type=multiple')
.then(res => {
const data = res.data.results[0];
const q = data.question;
const correct = data.correct_answer;
const incorrect = data.incorrect_answers;
this.setState({question:q,correct:correct,incorrect:incorrect})
})
}
evaluate(selected){
if(selected === this.state.correct){
this.setState({result: 'Correct!'})
} else {
this.setState({result:`Wrong! Correct Answer is ${this.state.correct}`})
}
}
render(){
const random = Math.floor(Math.random() * 3);
const options = this.state.incorrect;
options.splice(random, 0, this.state.correct);
const choices = options.map((option,i) => (
<div>
<input type='radio'
name='option'
value={option}
key={i} onChange={() => this.evaluate(option)}/>
<label for='option'>{option}</label>
</div>
) )
return(
<div>
<div>{this.state.question}</div>
<div>{choices}</div>
<div>{this.state.result}</div>
</div>
)
}
}
ReactDOM.render(<App />, appRoot)
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<div id='app'></div>
答案 0 :(得分:1)
这似乎是一个诚实的错误(我一贯有罪):
在您的render
方法中,您可以对数组进行突变:
const options = this.state.incorrect;
options.splice(random, 0, this.state.correct);
我怀疑您想要的是什么
const options = this.state.incorrect.slice(); //create a COPY of the array
options.splice(random, 0, this.state.correct);
React基本上是一种状态机,用于查找状态更改并智能地更新依赖于该状态的零件。但这只是JavaScript。通过直接拼接状态对象,您可以在render方法中更改状态。 React不知道您更改了状态,因此,您最终会遇到意想不到的行为,尤其是render
经常被调用。
我已经复制了您的代码段,以便您可以看到一个有效的示例。
const appRoot = document.getElementById('app');
class App extends React.Component{
state={
question:'',
correct:'',
incorrect:[],
result: null
}
componentDidMount(){
axios.get('https://opentdb.com/api.php?amount=1&type=multiple')
.then(res => {
const data = res.data.results[0];
const q = data.question;
const correct = data.correct_answer;
const incorrect = data.incorrect_answers;
this.setState({question:q,correct:correct,incorrect:incorrect})
})
}
evaluate(selected){
if(selected === this.state.correct){
this.setState({result: 'Correct!'})
} else {
this.setState({result:`Wrong! Correct Answer is ${this.state.correct}`})
}
}
render(){
const random = Math.floor(Math.random() * 3);
const options = this.state.incorrect.slice();
options.splice(random, 0, this.state.correct);
const choices = options.map((option,i) => (
<div>
<input type='radio'
name='option'
value={option}
key={i} onChange={() => this.evaluate(option)}/>
<label for='option'>{option}</label>
</div>
) )
return(
<div>
<div>{this.state.question}</div>
<div>{choices}</div>
<div>{this.state.result}</div>
</div>
)
}
}
ReactDOM.render(<App />, appRoot)
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<div id='app'></div>