1

I have the following navigation config file. This config file use react navigation to create navigators. My question is how can i call a function from my navigation config file that is define is another component such as a separate screen competent. In the below code i want to be able to call the showAlert() from the header button of the Map route. Please note that the showAlert() is define in the Map screen component below my navigator config.

const Tabs = createBottomTabNavigator ({
        Map:{screen:MapScreen},
        Profile:{screen:ProfileScreen},
        Consent: { screen: ConsentScreen },
     },
        {



        tabBarOptions: {
             activeTintColor: 'tomato',
             inactiveTintColor: 'gray',
           }
      }


);


Tabs.navigationOptions = ({ navigation }) => {
  const { routes, index } = navigation.state;
  const navigationOptions = {};
if (routes[index].routeName === 'Map') {
   navigationOptions.headerRight= (
                               <Button
                                 onPress={routes.showAlert()}
                                 title="Search"
                                 color="#009688"
                                 backgroundColor="white"
                                 icon={{name:'search',color:'#009688'}}
                               />
                             );

}



 if (routes[index].routeName === 'Profile') {
    navigationOptions.headerRight= (
                            <Button
                              onPress={()=> navigation.navigate('settingaccount')}
                              title="Settings"
                              color="#009688"
                              backgroundColor="white"
                              icon={{name:'settings',color:'#009688'}}
                            />
                          );
     navigationOptions.headerLeft= (
                                 <Button
                                   onPress={()=> navigation.navigate('profileEdit')}
                                   title="Edit Profile"
                                    icon={{name:'account-circle',color:'#009688'}}
                                    color="#009688"
                                    backgroundColor="white"
                                    color="#009688"
                                 />
                               );


 }

 if(routes[index].routeName === 'Consent'||routes[index].routeName === 'Map'){
 navigationOptions.headerLeft= null

 }

  return navigationOptions;
}

//Screen component

class Map extends React.Component {

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



  render() {
    return (
      <View />
    );
  }
}
2
  • Hey your problem is explained here: stackoverflow.com/questions/38567738/… What you need to do is access the child methods using "ref". Or you can call the method from the parent using the props of the child component. Commented Oct 25, 2018 at 13:06
  • Hi this is not the same issue, that issue was between two class however my tab bar navigator is not a class it’s just a configuration of react navigation routes that are exported and use within other places of the app. Commented Oct 25, 2018 at 15:45

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.