【问题标题】:react-native get current route namereact-native 获取当前路由名称
【发布时间】:2018-06-29 14:30:47
【问题描述】:

这是一篇“分享你的知识”的帖子。我需要这个功能并搜索了很多小时,所以我分享了我的解决方案。随意发帖抄送。

react native如何从主App获取当前路由名称?

编辑:这是针对 react-navigation V1

(知道我们不能在其中使用 this.props.navigation 因为它定义了 TabNavigator 和 StackNavigator 本身)

/** the trick is with DeviceEventEmitter **/
import { DeviceEventEmitter } from 'react-native';
const initialRouteName = "FluxObservations";


const MyTabView = TabNavigator(
  {
    FluxObservations: { screen: FluxObservations },
    Projects: { screen: Projects },
    NewObservation: { screen: NewObservation},
    Explorer: { screen: Explorer },
    Profil: { screen: Profil }
  },
  {
    swipeEnabled: false,
    initialRouteName: initialRouteName,
    tabBarPosition: "bottom",
    tabBarOptions: { ... },
    navigationOptions: {
      tabBarOnPress: ({ previousScene, scene, jumpToIndex }) => {
        //On each tab change we inject event
        DeviceEventEmitter.emit('updateCurrentRouteName', { currentRouteName: scene.route.routeName });
        // Keep original behaviour
        jumpToIndex(scene.index)
      }
    }
  }
);

const StackView = StackNavigator(
  {
    global: {
      screen: MyTabView,
      headerMode: "none",
      header: null,
      navigationOptions: {
        header: null
      }
    },     
    Credits: { screen: Credits },
    Floristic: { screen: Floristic },
    TermsOfUse: { screen: TermsOfUse },
    LegalNotice: { screen: LegalNotice },
    Options: { screen: Options },
  },
  {mode: 'modal'}
);

export default class App extends React.PureComponent {
  constructor (props) {
    super(props);
    // emit the event for the first tab opened by default, initialRouteName
    DeviceEventEmitter.emit('updateCurrentRouteName', { currentRouteName: initialRouteName });
  }


  // catching all event for route name change ! 
  listeners = {
    update: DeviceEventEmitter.addListener('updateCurrentRouteName', ({ ...currentRouteName }) => {
      console.log("MAIN ", currentRouteName) // Perform update
    })
  }


  render() {
    return (
      <Provider store={store}>
        <PersistGate
          loading={<ActivityIndicator />}
          persistor={persistor}>
          <View style={styles.fullScreen}>
            <StackView />
          </View>
        </PersistGate>
      </Provider>
    );
  }
}

每次用户点击它们时,我都需要在每个 TabNavigator 屏幕上显示教程模式(直到他点击“我明白了”)

【问题讨论】:

    标签: react-native react-navigation


    【解决方案1】:

    使用场景追踪方法detailed here,在官方文档中

    function getActiveRouteName(navigationState) {
      if (!navigationState) {
        return null;
      }
      const route = navigationState.routes[navigationState.index];
      // dive into nested navigators
      if (route.routes) {
        return getActiveRouteName(route);
      }
      return route.routeName;
    } 
    
    <StackView
      onNavigationStateChange={(prevState, currentState) => {
        const currentScreen = getActiveRouteName(currentState);
        console.log("currentScreen  : " + currentScreen);  
      }}
    />
    

    【讨论】:

    • 这个解决方案要容易得多,谢谢。我更新到反应导航 2.5.5。现在的问题是它不记录 tabNavigator 的initialRouteName
    • 正如您在official Redux integration 中看到的那样,您需要使用导航器本身获取initialRoute,这可能会让您知道该怎么做:class App { componentDidMount() { this.track('initialRoute'); } render() { return &lt;AppNavigator onNavigationStateChange={this.track} /&gt; } }; source
    猜你喜欢
    • 1970-01-01
    • 2016-05-09
    • 1970-01-01
    • 2017-11-28
    • 2018-07-05
    • 1970-01-01
    • 1970-01-01
    • 2015-09-14
    • 2019-11-23
    相关资源
    最近更新 更多