2017-05-25 93 views
2

我被困在navigationOptions裏面添加保存方法你能幫我做正確的方法嗎?React-Native導航中的OnPress導航選項

static navigationOptions = ({navigation}) => ({ 
     headerTitle: "Add New Item", 
     ...css.header, 
     headerRight: <NavViewRight 
      onPress={() => this.rightHeaderAction()} />, 
    }) 

回答

3

其實目前還不清楚你到底在做什麼。 但似乎你想從靜態方法調用類內的非靜態方法。

您提到this,但這裏this不是指類實例。爲了從你的類中調用某些東西,你需要使方法靜態。

事情是這樣的:

class MyScreen extends Component { 
    static navigationOptions = ({ 
     navigation 
    }) => ({ 
     headerTitle: "Add New Item", 
     ...css.header, 
     headerRight: < NavViewRight 
     onPress = { 
      () => MyScreen.rightHeaderAction() 
     } 
     />, 
    }) 

    static rightHeaderAction() { 
     // your code here 
    } 
}