2017-04-05 8 views
0

以下のReact Nativeコードを実行するときには、次のエラーが発生します。React Nativeコンポーネントクラスでオブジェクトを定義するときのエラー

Link to Error Screenshot

私はactionEnumオブジェクトを定義するためのさまざまな方法を試してみたが、何も動作していないようにみえます。助けてください!

import React from "react"; 
import { 
    View, 
    Text, 
    StyleSheet, 
    TextInput, 
    Button, 
    AsyncStorage 
} from "react-native"; 

class AddItem extends React.Component { 
    static navigationOptions = { 
    title: "Add item" 
    }; 

    static actionEnum = { 
    init: 1, 
    add: 2, 
    update: 3, 
    delete: 4, 
    set: 5 
    }; 

    constructor(props) { 
    super(props); 
    this.setStateHandler(this.actionEnum.add); 
    } 

.....

答えて

0

JavaScriptが唯一static methods静的変数を、持っていません。 enumオブジェクトをモジュールからクラスにエクスポートするのではなく、モジュールからエクスポートすることで回避できますが、実際にクラスの一部にする必要がある場合はget accessorを使用できます。

class AddItem extends React.Component { 
    static get navigationOptions() { 
    return { 
     title: "Add item" 
    }; 
    } 

    static get actionEnum() { 
    return { 
     init: 1, 
     add: 2, 
     update: 3, 
     delete: 4, 
     set: 5 
    }; 
    } 

    constructor(props) { 
    super(props); 
    this.setStateHandler(this.actionEnum.add); 
    } 
+0

ES6仕様ではありません...リアクト - ネイティブ – WilomGfx

+0

http://exploringjs.com/es6/ch_classes.html#_constructor-static-methods-prototype-methods – WilomGfx

+0

実際、ES6仕様で使用されています静的なデータプロパティ(変数)ではなく、静的メソッドのみを定義します。 "静的メソッド、ゲッター、セッターだけを作成できますが、静的データプロパティは作成できないため、ES6クラスを時間通りに仕上げるために意図的に"最小限に "設計しました。 あなた自身のリンクから。 –

関連する問題