I am using the new React-navigation from react-native. I have the navigation as follows:
StackNavigator:
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?
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).
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.
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.
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> ) } }
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', {});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With