簡體   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