我的handleClick 事件没有按预期工作

发布于 2025-01-11 02:53:19 字数 1237 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柏林苍穹下 2025-01-18 02:53:19

您能否发布整个代码,因为除了重构以优化一点之外,我不明白为什么它不起作用。

优化:

//before return
const { details, city, temp, feels, image } = this.state
// after return
onClick={() => this.setState({ details: !details })

Would you please post the entire code since besides refactoring to optimize a bit I don't see why it doesn't work.

Optimization:

//before return
const { details, city, temp, feels, image } = this.state
// after return
onClick={() => this.setState({ details: !details })
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文