简体   繁体   中英

Getters in ES6 functional component React.js

I am using stateless component in React and I found issue with using 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.

Check the reference .

A getter can only be defined as a property of an object or a class. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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