繁体   English   中英

ES6 函数式组件 React.js 中的 Getter

[英]Getters in ES6 functional component React.js

我在 React 中使用无状态组件,我发现使用 Getters 有问题。

对于有状态组件(基于类的组件),它可以正常工作,但是如何在无状态(功能组件)中使用它;

 // 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'.

您只能在 ES6 类和对象字面量中使用getset关键字。

检查参考

getter 只能定义为对象或类的属性。 您不能直接在函数体中定义它们。

您要么需要用普通函数(可能是更好的解决方案)替换lookupsOfSelectedGroup ,要么将其包装在对象文字中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM