13

Hello I 'm trying to bind a function in my Navigator Right Button,

But It gives error.

This is my code:

import React, { Component } from 'react';
import Icon from 'react-native-vector-icons/FontAwesome';
import Modal from 'react-native-modalbox';
import { StackNavigator } from 'react-navigation';
import {
   Text,
   View,
   Alert,
   StyleSheet,
   TextInput,
   Button,
   TouchableHighlight
} from 'react-native';

import NewsTab from './tabs/news-tab';
import CustomTabBar from './tabs/custom-tab-bar';

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

    alertMe(){
        Alert.alert("sss");
    }

    static navigationOptions = {
        title: 'Anasayfa',
        headerRight: 
            (<TouchableHighlight onPress={this.alertMe.bind(this)} >
                <Text>asd</Text>
             </TouchableHighlight>)        
    };

    render() {
        return(
            <View>

            </View>
        );
    }
}

And Get error like this:

undefined is not an object (evaluating 'this.alertMe.bind')

When I use this method in render function it is working great but in NavigatonOption I cant get handled it. what can I do for this problem.

Ioana B
  • 195
  • 3
  • 16
Andaç Temel
  • 216
  • 1
  • 2
  • 9

4 Answers4

52

You should use this in you navigator function

static navigationOptions = ({ navigation }) => {
    const { params = {} } = navigation.state;
    return {
        title: '[ Admin ]',
        headerTitleStyle :{color:'#fff'},
        headerStyle: {backgroundColor:'#3c3c3c'},
        headerRight: <Icon style={{ marginLeft:15,color:'#fff' }} name={'bars'} size={25} onPress={() => params.handleSave()} />
    };
};

use the componentwillmount so that it can represent where you are calling function .

componentDidMount() {
this.props.navigation.setParams({ handleSave: this._saveDetails });
}

and then you can write your logic in the function

_saveDetails() {
**write you logic here for **
}

**no need to bind function if you are using this **

HpDev
  • 2,789
  • 1
  • 15
  • 20
3

May be same as above ...

   class LoginScreen extends React.Component {
      static navigationOptions = {
        header: ({ state }) => ({
          right: <Button title={"Save"} onPress={state.params.showAlert} />
        })
      };

      showAlert() {
        Alert.alert('No Internet',
          'Check internet connection',
          [
            { text: 'OK', onPress: () => console.log('OK Pressed') },
          ],
          { cancelable: false }
        )
      }

      componentDidMount() {
        this.props.navigation.setParams({ showAlert: this.showAlert });
      }

      render() {
        return (
          <View />
        );
      }
    }
Sanjeev Rao
  • 2,247
  • 1
  • 19
  • 18
3

react-navigation v5 version would be:

export const ClassDetail = ({ navigation }) => {
  const handleOnTouchMoreButton = () => {
    // ...
  };

  useLayoutEffect(() => {
    navigation.setOptions({
      headerRight: () => (
        <TouchableOpacity onPress={handleOnTouchMoreButton}>
          <Icon name="more" />
        </TouchableOpacity>
      ),
    });
  }, [navigation]);

  return (
    // ...
  )
}
Kutsan Kaplan
  • 1,755
  • 3
  • 14
  • 22
1

This is for navigation v4. You need to modify navigationoptions outside of the functional component. Your button event needs to pass a param via navigation.

pageName['navigationOptions'] = props => ({
    headerRight:  ()=>
         <TouchableOpacity onPress={() => props.navigation.navigate("pageRoute", 
{"openAddPopover": true})  } ><Text>+</Text></TouchableOpacity>    })

and then in your functional component, you can use that param to do something like this:

useLayoutEffect(() => {
   doSomethingWithYourNewParamter(navigation.getParam("openAddPopover"))
}, [navigation])
chris
  • 9
  • 1