我的handleClick 事件没有按预期工作
handleClick 的功能是简单地执行 this.setState({details: true})
,当它已经为 true 时反之亦然。控制台日志显示每次单击时详细信息的状态都会在 true 和 false 之间变化,但该行仅显示“无法调用详细信息”
const retval = (
<div>
<hr />
<div className="weather-card">
<h3>City: {this.state.city}</h3> <img onClick={() => this.handleClick()} src={this.state.image} />
<h3>
Temprature: {this.state.temp} Feels Like: {this.state.feels}
</h3>
<p>{this.state.w_status}</p>
<p>{this.state.description}</p>
<div id="details" className="weather-details">
{this.state.details ? "well well well": "Couldnt call the details"}
</div>
</div>
<hr />
</div>
);
async handleClick(e) {
if(this.state.details == true){
this.setState({ details: false});
console.log("It is hidden");
}
else{
this.setState({ details: true});
console.log("It is shown");
}
}
The functionality of the handleClick is to simply do this.setState({details: true})
and vice-versa when it is already true. The console log shows that the state of details is changing between true and false on every click however the line only shows "Couldnt call the details"
const retval = (
<div>
<hr />
<div className="weather-card">
<h3>City: {this.state.city}</h3> <img onClick={() => this.handleClick()} src={this.state.image} />
<h3>
Temprature: {this.state.temp} Feels Like: {this.state.feels}
</h3>
<p>{this.state.w_status}</p>
<p>{this.state.description}</p>
<div id="details" className="weather-details">
{this.state.details ? "well well well": "Couldnt call the details"}
</div>
</div>
<hr />
</div>
);
async handleClick(e) {
if(this.state.details == true){
this.setState({ details: false});
console.log("It is hidden");
}
else{
this.setState({ details: true});
console.log("It is shown");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您能否发布整个代码,因为除了重构以优化一点之外,我不明白为什么它不起作用。
优化:
Would you please post the entire code since besides refactoring to optimize a bit I don't see why it doesn't work.
Optimization: