reactjs - IonChange 上的 Ionic React IonRadioGroup 无限循环
问题描述
每当我在组件更新上设置单选按钮组的值时,我都会得到一个无限循环,我正在开发一个让我卡在这里的测验应用程序是我当前的代码,我想要一个用户已经解决的情况一个测验问题 导航到该问题时应该已经选择了答案
xport default class SubjectQuiz extends React.Component <IProps, IState>{
constructor(props: IProps) {
super(props);
this.state = { userSelectedAnswers:[], score: 0,curNum:0,
quizOver:false,
Questions:this.props.Question,
selected:'',
currentSelectedIndex:{questionNumber:0,
option:'',
//index:string|number,
answerIsCorrect:false,
answer:''}};
}
componentDidUpdate(){
var i = this.state.userSelectedAnswers.findIndex(o => o.questionNumber === this.state.curNum);
console.log("the value of question index in array "+i);
if(i==-1){
this.setState({selected:'F'})
}else{
this.setState({selected:this.state.userSelectedAnswers[i].correctAnswerOption})
}
}
这里是 TSX
<IonRadioGroup value={this.state.selected} onIonChange={e=>{
this.setState({selected:e.detail.value})
this.answerSelected(this.state.selected);
console.log("here is the selected answer "+JSON.stringify(e.detail));
}}>
<IonList>
<IonItem>
<p dangerouslySetInnerHTML={{__html: this.parseHtmlString(this.state.Questions[this.state.curNum].option_a,this.ImageUrl).outerHTML}}></p>
<IonRadio slot="start" value="A" />
</IonItem>
<IonItem>
<p dangerouslySetInnerHTML={{__html: this.parseHtmlString(this.state.Questions[this.state.curNum].option_b,this.ImageUrl).outerHTML}}></p>
<IonRadio slot="start" value="B" />
</IonItem>
<IonItem>
<p dangerouslySetInnerHTML={{__html: this.parseHtmlString(this.state.Questions[this.state.curNum].option_c,this.ImageUrl).outerHTML}}></p>
<IonRadio slot="start" value="C" />
</IonItem>
<IonItem>
<p dangerouslySetInnerHTML={{__html: this.parseHtmlString(this.state.Questions[this.state.curNum].option_d,this.ImageUrl).outerHTML}}></p>
<IonRadio slot="start" value="D" />
</IonItem>
{this.state.Questions[this.state.curNum].option_e?
<IonItem>
<p dangerouslySetInnerHTML={{__html: this.parseHtmlString(this.state.Questions[this.state.curNum].option_e,this.ImageUrl).outerHTML}}></p>
<IonRadio slot="start" value="E" />
</IonItem>:null}
<IonRadio slot="start" value="F" defaultChecked={true} hidden={true} />
</IonList>
</IonRadioGroup>
解决方案
正如您在react doc中看到的防止无限循环的那样,您应该将 prevState 与当前状态进行比较,然后进行更改:
componentDidUpdate(prevProps,prevState){
// Typical usage (don't forget to compare props):
if (this.state.selected!== prevState.selected) {
var i = this.state.userSelectedAnswers.findIndex(o => o.questionNumber === this.state.curNum);
console.log("the value of question index in array " + i);
if (i == -1) {
this.setState({ selected: 'F' })
} else {
this.setState({ selected: this.state.userSelectedAnswers[i].correctAnswerOption })
}
}
}
推荐阅读
- angular - 是否可以在 weblogic 12c 中部署 Angular 应用程序?
- python - 如何在多个 GPU 之间拆分一个 Keras 模型
- php - php7.2.7中缺少mcrypt模块打开功能
- reactjs - 如何修复“400 Bad Request”rest-auth/registration/?
- python - 如何在已经具有复杂数据类型的数组的对角线上生成复数?
- rest - Acumatica - 我们如何通过“0”价格的 Acumatica 订单项
- winforms - 如何再次看到失控?
- html - 在 django 模板中使用变量作为 dict 键
- java - 将现有项目传递给 androidx 让我无法解析 com.google.android:support-v4:r11
- swift - 我越来越 ”
:0: 错误:无法在 cygwin 上加载 swift 标准库