如何在不使用导航道具的情况下在路线之间导航?

问题描述 投票:0回答:1

我正在尝试从DrawerOpen内部的屏幕(示例#3)调用StackNavigator路径(示例#1),<ProductStack>在组件内呈现为DrawerOpen(示例#3)。

在Stack中,我需要一个能够访问DrawerOpen的按钮,但是我在该页面上的导航无法访问import React, { Component } from 'react' import { DrawerNavigator } from 'react-navigation' import ProductEntryScreen from './../productEntry/screen' import InvoiceInScreen from './../invoiceIn/screen' const InvoiceInEntry = { screen: InvoiceInScreen} const ProductEntry = { screen: ProductEntryScreen, navigationOptions: { title: 'Entrada de produto' } } const Drawer = DrawerNavigator({ ProductEntry, InvoiceInEntry }) export default Drawer 路径。

有没有办法从我的应用程序的任何组件访问(和调用)任何路由,而不依赖于导航道具?

这是我的抽屉组件(示例#1):

ProductEntryScreen

这是包装器组件(示例#2,它也作为import React, { Component } from 'react' import { StackNavigator, } from 'react-navigation' import InfoScreen from './info/screen' /* Other imports omitted due to brevity */ import * as ProductEntryActions from './actions' import { bindActionCreators } from 'redux' import { connect } from 'react-redux' const Info = { screen: InfoScreen, navigationOptions: { title: 'Entrada de produto' } } /* Other routes omitted due to brevity */ const ProductStack = StackNavigator({ Info, /* Other routes omitted due to brevity */ },{ initialRouteName: 'Info', }) class ProductEntryScreen extends Component { componentDidMount() { this.props.getBlocks() } render() { return ( <ProductStack /> ) } } const mapDispatchToProps = dispatch => bindActionCreators(ProductEntryActions, dispatch) export default connect(null, mapDispatchToProps)(ProductEntryScreen) 导入#1):

import React, { Component } from 'react'

/* Imports ommited */

export default class ProductEntryScreen extends Component {
  static navigationOptions = ({navigation, screenProps}) => ({
    headerLeft: (
      <MenuButton
        onClick={() => {
          // This used to work without wrapping ProductStack in a component
          // but I need it to be a component
          navigation.navigate('DrawerOpen')
        }
      />
    ),
  })

  render() {
    return (
      <View></View>
    )
  }
}

现在屏幕(示例#3):

export default ProductStack

如果我在没有创建组件的情况下在#2上进行componentDidMount(也在DrawerOpen上失去了该功能),那么导航到#3上的DrawerOpen就像魅力一样。但我需要调用该函数,并且还知道一种方法来调用navigation而不必依赖于NavigationActions道具。

react-native react-navigation
1个回答
0
投票

如果您正在使用redux或类似的东西,那么您可以创建一个动作来为您路由。如果没有,你可以使用import { NavigationActions } from 'react-navigation'; const navigateAction = NavigationActions.navigate({ routeName: 'Profile', params: {}, }); this.props.navigation.dispatch(navigateAction);

qazxswpoi
© www.soinside.com 2019 - 2024. All rights reserved.