8

我正在使用来自https://reactnavigation.org/docs/navigators/tab的 React Navigation 的选项卡导航器,当我在选项卡屏幕之间切换时,我在 this.props.navigation 中没有任何导航状态。

选项卡导航器

    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
      }
    });

仪表板屏幕:

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>
    );
  }
}

当它首先呈现组件时,我在仪表板屏幕上获得道具,但在切换选项卡时我没有得到道具。我需要获取当前的屏幕名称,即今天或本周。

4

3 回答 3

14

您的问题与“屏幕跟踪”有关,react-navigation对此有官方指南。如果你想与 Redux 集成,你可以使用onNavigationStateChange内置的导航容器来跟踪屏幕,或者编写一个 Redux 中间件来跟踪屏幕。更多详细信息可以在官方指南中找到:Screen-Tracking。以下是使用指南中的示例代码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);
      }
    }}
  />
);
于 2017-07-13T09:15:45.710 回答
6

首先检查所有属性,例如

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

上面的 json 数组将显示 routeName 索引下的当前导航状态,即

this.props.navigation.state.routeName
于 2018-09-25T11:16:13.207 回答
1

您是否尝试在路由对象中定义 navigationOptions?

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

您还可以将 navigationOptions 设置为将与导航对象一起调用的回调。

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

阅读有关导航选项的更多信息https://reactnavigation.org/docs/navigators/

于 2017-07-13T09:44:28.353 回答