【发布时间】:2019-12-07 15:28:23
【问题描述】:
我在 React 中使用无状态组件,但发现使用 Getters 存在问题。
对于 statefull 组件(基于类的组件),它可以正常工作,但是我如何在无状态(功能组件)中使用它;
// this is code for statefull component(class based component)
get lookupsOfSelectedGroup(){
const lookUps = this.props.mainLookups.filter(
item => item.extras.parent === this.state.activeGroup
);
if (lookUps[0] && lookUps[0].responseStatus === 200) {
return lookUps[0].response.lookup;
}
return [];
}
// this is the code for functional component I did:
get lookupsOfSelectedGroup =()=> {
const lookUps = this.props.mainLookups.filter(
item => item.extras.parent === this.state.activeGroup
);
if (lookUps[0] && lookUps[0].responseStatus === 200) {
return lookUps[0].response.lookup;
}
return [];
} ```
Cannot find name 'get'.
【问题讨论】:
-
你试过
get lookupsOfSelectedGroup(){ ... } -
无状态组件没有实例,你希望如何调用这个getter?此外,您的
get lookupsOfSelectedGroup = arrow function即使对于基于类的组件也不起作用。
标签: javascript reactjs getter-setter getter