Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native call function from navigation

I'm using React native navigation. (Stack Navigation). But I can't call function in navigationOptions. Not working.

import React, { Component } from 'react';
import { StyleSheet, View, Text, TouchableHighlight, AsyncStorage, Alert } from 'react-native';
import { Button } from 'react-native-elements';
import Icon from 'react-native-vector-icons/FontAwesome';
import HandleBack from '../../HandleBack';

export default class Dashboard extends Component {
    constructor(props) {
        super(props);
    }

    static navigationOptions = ({ navigation }) => {
        return {
            title: 'Dasboard',
            headerLeft: null,
            headerRight: (
                <TouchableHighlight underlayColor='transparent' onPress={this.login.bind(this)} style={{marginRight: 10}}>
                    <Icon
                        name="power-off"
                        size={25}
                        color="white"
                    />
                </TouchableHighlight>
            )
        };
    };

    login() {
        alert('Button clicked!');
    }

    onBack = () => {
        this.props.navigation.navigate('Screen3');
    };    

    render() {        
        return(
            <HandleBack onBack={ this.onBack }>
                <View>
                    <Text> This is screen 2 </Text>
                    <TouchableHighlight onPress={() => this.props.navigation.navigate('Screen3')}> 
                        <Text> Go to Screen 3 </Text>
                    </TouchableHighlight>
                </View>
            </HandleBack>
        )
    }
}

When I'm using onPress={this.login.bind(this)} get error

"TypeError: TypeError: undefined is not an object (evaluatinh '_class.login.bind')"

When I'm using onPress={this.login} no reaction.

When I'm using onPress={this.login()} get error

TypeError: TypeError: _class.login is not a function.

But

I'm using onPress={() => alert('test')} is working.

like image 629
Gündoğdu Yakıcı Avatar asked Dec 14 '22 12:12

Gündoğdu Yakıcı


1 Answers

you can achieve it using setParams or getParams for react-navigation.

export default class Dashboard extends Component {


    static navigationOptions = ({ navigation }) => {
        return {
            title: 'Dasboard',
            headerLeft: null,
            headerRight: (
                <TouchableHighlight underlayColor='transparent' 
                 onPress={navigation.getParam('login')} //call that function in onPress using getParam which we already set in componentDidMount
                   style={{marginRight: 10}}>
                    <Icon
                        name="power-off"
                        size={25}
                        color="white"
                    />
                </TouchableHighlight>
            )
        };
    };
   login() {
        alert('login click')
    }
    onBack = () => {
        this.props.navigation.navigate('Screen3');
    };    

componentDidMount() {
        this.props.navigation.setParams({ login: this.login }); //initialize your function
    }
    render() {        
        return(
          .....
        )
    }
}
like image 106
Patel Dhara Avatar answered Dec 21 '22 12:12

Patel Dhara