[英]Getters in ES6 functional component React.js
I am using stateless component in React and I found issue with using Getters.我在 React 中使用无状态组件,我发现使用 Getters 有问题。
for the statefull component (class based component) it is properly working but how can I use this in stateless(functional component);对于有状态组件(基于类的组件),它可以正常工作,但是如何在无状态(功能组件)中使用它;
// 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'.
You can only use the get
and set
keywords in ES6 classes and object literals.您只能在 ES6 类和对象字面量中使用get
和set
关键字。
A getter can only be defined as a property of an object or a class. getter 只能定义为对象或类的属性。 You cannot define them directly in the body of a function.您不能直接在函数体中定义它们。
You will either need to replace lookupsOfSelectedGroup
with a plain function (probably the better solution), or wrap it in an object literal.您要么需要用普通函数(可能是更好的解决方案)替换lookupsOfSelectedGroup
,要么将其包装在对象文字中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.