gpt4 book ai didi

javascript - 组件不再渲染 React JS

转载 作者:行者123 更新时间:2023-11-30 06:22:34 25 4
gpt4 key购买 nike

这是一个简单的 React JS 程序,当我更新它正在运行的渲染方法而不是再次重新渲染组件时

这是更新状态的方式

this.setState({q_no : this.state.q_no +1 });

渲染方法

render(){
const {loading, q_no, questions} = this.state;
return(
<div className="container cond7sdagF">
<h3><i>{this.quizInd.name}</i></h3>
{!loading && <h6>Loading</h6>}
{loading && <Question question={questions[q_no]} nextQuestion={this.nextQuestion} />}
</div>
)
}

最佳答案

不确定是不是打错了,但看来你的条件倒退了:

这个

        {!loading && <h6>Loading</h6>}
{loading && <Question question={questions[q_no]} nextQuestion={this.nextQuestion} />}

应该是这样

            {loading && <h6>Loading</h6>}
{!loading && <Question question={questions[q_no]} nextQuestion={this.nextQuestion} />}

另一件事,当你的新状态基于之前的状态时,its better to use the functional version setState:

this.setState(state => ({q_no : state.q_no +1 }));

关于javascript - 组件不再渲染 React JS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52314910/

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