Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handle OnPress inside React-Native navigationOptions

I'm stuck in adding save method inside navigationOptions can you please help me to do right way.

static navigationOptions = ({navigation}) => ({
        headerTitle: "Add New Item",
        ...css.header,
        headerRight: <NavViewRight
            onPress={() => this.rightHeaderAction()} />,
    })
like image 382
maddy Avatar asked May 25 '17 11:05

maddy


1 Answers

Actually it's not clear what exactly you try to do. But seems like you want to call a non-static method inside class from static method.

You referring to this, but this here not means class instance. In order to call something from your class you need to make method static.

Something like this:

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

    static rightHeaderAction() {
        // your code here
    }
}
like image 107
Alexei Malashkevich Avatar answered Nov 15 '22 11:11

Alexei Malashkevich