静态函数中的this.state反应

时间:2019-02-09 10:36:08

标签: reactjs jsx

我有以下代码:

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}%` }} />
);

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

1 个答案:

答案 0 :(得分:1)

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

签出Call static from class