Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Navigate to different screen from a button in a header

Tags:

I am using the new React-navigation from react-native. I have the navigation as follows:

StackNavigator:

  1. TabNavigator // HomeNavigation
  2. TabNavigator // NotificationNavigation

Full code:

const MainNavigation = StackNavigator({     Home: {         screen: HomeNavigation,     },     Notification: {         screen: NotificationNavigation,     } });  const HomeNavigation = TabNavigator({     AllPost: {         screen: All,     },     ArticlePost: {         screen: Article,     },     BusinessPost: {         screen: Job,     }, });  HomeNavigation.navigationOptions = {     title: 'Home',     header: {         right: <SearchNotification/>     }, };  class SearchNotification extends React.Component {     goToNotification = () => {         this.props.navigate('Notification');     };      render() {         return (             <View style={styles.container}>                 <TouchableOpacity>                     <Icon name="md-search" style={styles.Icon}/>                 </TouchableOpacity>                 <TouchableOpacity style={styles.notificationWrapper} onPress={this.goToNotification}>                     <Icon name="md-notifications" style={styles.Icon}/>                     <Text style={styles.number}>3</Text>                 </TouchableOpacity>             </View>         )     } }  const NotificationNavigation = TabNavigator({     Comment: {         screen: NotificationComment,     },     Follow: {         screen: NotificationFollow,     } }); 

HomeNavigation has a header, and the header has a right component of SearchNotification. SearchNotification has an icon which on press I would like to go to the NotificatoinNavigation.

However, if I make changes to the header of HomeNavigation to this way, the SearchNotification is not displayed in the header.

HomeNavigation.navigationOptions = {     title: 'Home',     header: {         tintColor: 'white',         style: {             backgroundColor: '#2ec76e',         },         right: ({navigate}) => <SearchNotification navigate={navigate}/>     }, }; 

How can I navigate to different screen from a button in a header?

like image 217
Kakar Avatar asked Feb 10 '17 21:02

Kakar


People also ask

How do I use the react navigation header buttons?

The most common way to interact with a header is by tapping on a button either to the left or the right of the title. Let's add a button to the right side of the header (one of the most difficult places to touch on your entire screen, depending on finger and phone size, but also a normal place to put buttons).

How do I navigate from one tab to another tab?

On Windows, use Ctrl-Tab to move to the next tab to the right and Ctrl-Shift-Tab to move to the next tab to the left.

How do I navigate from header in react-native?

To configure the header bar of a React Native application, the navigation options are used. The navigation options are a static property of the screen component which is either an object or a function. headerTitle: It is used to set the title of the active screen. headerStyle: It is used to add style to the header bar.


2 Answers

So the problem was (I think), inside the navigationOptions instead of using navigations I had to use navigate, and pass it as a props to the child (i.e. the SearchNotification).

So the final code looks like this:

HomeNavigation.navigationOptions = {     title: 'Home',     header: ({navigate}) => ({         right: (             <SearchNotification navigate={navigate}/>         ),     }), }; 

And within the SearchNotification component:

export default class SearchNotification extends React.Component {     goToNotification = () => {         this.props.navigate('Notification');     };      render() {         return (             <View style={styles.container}>                 <TouchableOpacity>                     <Icon name="md-search" style={styles.Icon}/>                 </TouchableOpacity>                 <TouchableOpacity style={styles.notificationWrapper}                                   onPress={() => this.props.navigate('Notification')}>                     <Icon name="md-notifications" style={styles.Icon}/>                     <Text style={styles.number}>3</Text>                 </TouchableOpacity>             </View>         )     } } 
like image 103
Kakar Avatar answered Oct 04 '22 10:10

Kakar


Code that worked for me:

import Header from '../Containers/Header' ......................................... navigationOptions: ({ navigation }) => ({       title: 'Find User',       headerRight: <Header navigation={navigation} />,       headerStyle: styles.header     }) 

And to move to other screen:

this.props.navigation.navigate('UserDetail', {}); 
like image 38
me_astr Avatar answered Oct 04 '22 11:10

me_astr