繁体   English   中英

静态函数中的this.state反应

[英]this.state inside static function in react

我有以下代码:

constructor(props) {
    super(props);
    this.state = {
      percentage: 60,
    };

还有一个静态函数,我想通过this.state.percentage- >使用此百分比

static get progressBar(){
   <div>
     <ProgressBar percentage={this.State.percentage} />
   </div>
}

但它显示错误是

意外的“ this”(mysticatea / no-this-in-static)

同样在此代码下面,它显示错误,这是

“道具验证中缺少百分比”

const ProgressBar = props => (
  <div className="progress-bar">
    <Filler percentage={props.percentage} />
  </div>
);

const Filler = props => (
  <div className="filler" style={{ width: `${props.percentage}%` }} />
);

我不明白为什么会显示这些错误。 而且由于我是新来的反应者,所以我无法解决问题。

静态上下文是在创建可用实例之前创建的,因此静态方法无法像其他任何语言(如Java等)那样在静态方法中使用。

检查一下从类中调用static

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM