Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get current navigation state

I am using React Navigation's Tab Navigator from https://reactnavigation.org/docs/navigators/tab, when I switch between the Tab Screens I don't get any navigation state in this.props.navigation.

Tab Navigator:

    import React, { Component } from 'react';
    import { View, Text, Image} from 'react-native';
    import DashboardTabScreen from 'FinanceBakerZ/src/components/dashboard/DashboardTabScreen';
    import { TabNavigator } from 'react-navigation';


       render() {
        console.log(this.props.navigation);   

        return (
          <View>
              <DashboardTabNavigator  />
          </View>
        );
      }



    const DashboardTabNavigator = TabNavigator({
      TODAY: {
        screen: DashboardTabScreen
      },
      THISWEEK: {
        screen: DashboardTabScreen
      }
    });

DASHBOARD SCREEN:

import React, { Component } from 'react';
import { View, Text} from 'react-native';

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

    this.state = {};
    console.log('props', props);

  }

  render() {
    console.log('props', this.props);
    return (
      <View style={{flex: 1}}>
        <Text>Checking!</Text>
      </View>
    );
  }
}

I get props at Dashboard Screen when it renders the component first but I don't get props when I switch the tabs. I need to get the current Screen name i.e TODAY or THISWEEK.

like image 966
Ahsan Ali Avatar asked Mar 16 '17 07:03

Ahsan Ali


3 Answers

Your problem is about "Screen Tracking", react-navigation has an officially guide for this. you can use onNavigationStateChange to track the screen by using built-in navigation container or write a Redux middleware to track the screen if you want to integrate with Redux. More detail can be found at the officially guide: Screen-Tracking. Below is a sample code from the guide by using onNavigationStateChange:

import { GoogleAnalyticsTracker } from 'react-native-google-analytics-bridge';

const tracker = new GoogleAnalyticsTracker(GA_TRACKING_ID);

// gets the current screen from navigation state
function getCurrentRouteName(navigationState) {
  if (!navigationState) {
    return null;
  }
  const route = navigationState.routes[navigationState.index];
  // dive into nested navigators
  if (route.routes) {
    return getCurrentRouteName(route);
  }
  return route.routeName;
}

const AppNavigator = StackNavigator(AppRouteConfigs);

export default () => (
  <AppNavigator
    onNavigationStateChange={(prevState, currentState) => {
      const currentScreen = getCurrentRouteName(currentState);
      const prevScreen = getCurrentRouteName(prevState);

      if (prevScreen !== currentScreen) {
        // the line below uses the Google Analytics tracker
        // change the tracker here to use other Mobile analytics SDK.
        tracker.trackScreenView(currentScreen);
      }
    }}
  />
);
like image 157
ufxmeng Avatar answered Nov 14 '22 17:11

ufxmeng


Check all properties first, like

<Text>{JSON.stringify(this.props, null, 2)}</Text>

Above json array will show you current state of navigation under routeName index i.e.

this.props.navigation.state.routeName
like image 29
vinod Avatar answered Nov 14 '22 17:11

vinod


Have you tried to define navigationOptions in your route object?

const DashboardTabNavigator = TabNavigator({
  TODAY: {
    screen: DashboardTabScreen
    navigationOptions: {
     title: 'TODAY',
    },
  },
})

You can also set navigationOptions to a callback that will be invoked with the navigation object.

const DashboardTabNavigator = TabNavigator({
  TODAY: {
    screen: DashboardTabScreen
    navigationOptions: ({ navigation }) => ({
     title: 'TODAY',
     navigationState: navigation.state,
    })
  },
})

Read more about navigationOptions https://reactnavigation.org/docs/navigators/

like image 28
devlighted Avatar answered Nov 14 '22 16:11

devlighted