我正在尝试将基于类的组件迁移到功能组件。
在变更中不断挣扎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" );
}
...
}
只需将其转换为普通变量,就不需要它作为类成员
const someGetter = () => flag
usingGetter () {
if (someGetter()) console.log("using Getter called! ");
}