gpt4 book ai didi

javascript - radio 输入重复选择( react )

转载 作者:行者123 更新时间:2023-11-30 20:13:48 28 4
gpt4 key购买 nike

我正在尝试通过在 codepen 上使用react来制作一个单一问题的测验应用程序。我正在使用 api 来获取问题、3 个错误答案和问题的正确答案并将其添加到状态。我在验证答案时遇到问题。单击 4 个选项之一后,我想有条件地渲染一个带有结果的 div。但问题是在显示结果后,另一个 radio 输入正在呈现。

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>

最佳答案

这似乎是一个无心之失(我一直犯的这个错误):

在你的 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。通过直接拼接状态对象,您正在更改渲染方法中的状态。 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>

关于javascript - radio 输入重复选择( react ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52128976/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com