【问题标题】:How to migrate getter function in class based component to functional component?如何将基于类的组件中的 getter 函数迁移到功能组件?
【发布时间】:2020-10-07 02:24:47
【问题描述】:

我正在尝试将基于类的组件迁移到功能组件。

不断努力改变getter / static方法,我该如何实现这些?

// class based component

class Example extends Component {
  ... 
  flag = Math.random * 1 + 1; 
  get someGetter(){
    return flag
  }
  usingGetter(){
    if(this.someGetter) console.log("using Getter called! ");
  }
  static someStatic(){
    console.log("this is some static function" );
  }
  ...
}

【问题讨论】:

  • 没有“银弹”,在你的IDE中使用多重编辑功能

标签: reactjs migration react-component react-functional-component


【解决方案1】:

只需将它转换为普通变量,你不需要它作为类成员

const someGetter = () => flag
usingGetter () {
  if (someGetter()) console.log("using Getter called! ");
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-27
    • 2020-08-18
    • 2020-09-28
    • 2021-11-12
    相关资源
    最近更新 更多