我有一个 onClick 方法来做出反应,它改变了一个名为指针的属性的状态。该功能在功能级别上工作,但是除非我双击按钮(onClick 方法),否则 UI 不会随我的状态更新。我尝试将组件转换为类组件以利用渲染方法,但仍然没有结果。
let questions = [
{
id: 1,
title: "This person tends to be quiet"
},
{
id: 2,
title: "This person is compassionate and has a soft heart."
},
{
id: 3,
title: "This person is disorganized.."
}
]
export default class Quiz extends React.Component {
constructor() {
super();
this.state = {
pointer: 0,
currentAnswer: ""
};
}
render() {
return (
<div>
<h1> {questions[this.state.pointer].title}</h1>
<button onClick={(e) => {
e.preventDefault();
this.setState({
pointer: this.pointer++
});
console.log(this.pointer);
this.forceUpdate();
}}>
</div>
)
}