简体   繁体   English

从抽屉导航器到标签导航器反应本机传递参数

[英]React Native Pass Param From Drawer Navigator to Tab Navigator

I have a tab navigator inside a drawer navigator and I want to pass data from the draw navigator to a page in the tab navigator. 我在抽屉式导航器中有一个选项卡导航器,我想将数据从绘图导航器传递到选项卡导航器中的页面。

I tried passing a parameter in the DrawerContainer.js using 我尝试使用以下方法在DrawerContainer.js中传递参数

this.props.navigation.navigate('TabsNav', {testParam: 'TEST'})

and retrive it by this.props.navigation.getParam in the HomePage screen. 并通过“首页”屏幕中的this.props.navigation.getParam对其进行this.props.navigation.getParam But this did not work because the its navigating to the TabNav then rendering the HomePage. 但这没有用,因为它导航到TabNav然后呈现了HomePage。

How do I pass data to the tab navigator form the DrawerContainer.js then to the HomePage screen. 如何将数据从DrawerContainer.js传递到选项卡导航器,然后传递到“主页”屏幕。

TabNav.js TabNav.js

export const Tabs = createMaterialTopTabNavigator(
  {
    HomePage: {
      screen: Home,
    },
    ListView: {
      screen: List,
    },
  },

  {
    order: ['HomePage', 'ListView'],
  },
)

DrawerContainer.js DrawerContainer.js

  render() {
    return (
      <View style={styles.container}>

        <View>
          <TouchableHighlight
            style={styles.TouchableHighlight}
            onPress={this.props.navigation.navigate('TabsNav', {testParam: 'TEST'})}

            <Text>Home</Text>
          </TouchableHighlight>
        </View>

        <View>
          <TouchableHighlight
            style={styles.TouchableHighlight}
            onPress={this.props.navigation.navigate('ProfilePage')}>

            <Text>List View</Text>
          </TouchableHighlight>
        </View>


      </View>
    )
  }
}

DrawNav.js DrawNav.js

import { Tabs } from './TabNav.js'
import Profile from '../ProfilePage.js'
import DrawerContainer from '../DrawerContainer'

export const Draw = createDrawerNavigator(
  {
    TabsNav: {screen: Tabs},
    ProfilePage: {screen: Profile},
  },
  {
    contentComponent: DrawerContainer
  },


);

HomePage.js HomePage.js

export default class HomePage extends Component{

  componentWillMount(){
    console.log(this.props.navigation.getParam('testParam', null))
  }

  render(){
     <View>

     </View>

  }
}

you need to navigate to a screen specifically inside of the tab, not to the tab itself, or the params go into the tab navigator. 您需要导航到选项卡内部的特定屏幕,而不是选项卡本身,否则参数将进入选项卡导航器。 params only go to the route that you are navigating to directly. 参数只会转到您直接导航到的路线。

so 所以

Drawer({
  TabsNav: Tab({
     HomePage: ScreenA,
     ListView: ScreenB,  
  })
});

navigate to HomePage or ListView rather than TabsNav 导航到HomePage或ListView而不是TabsNav

this.props.navigation.navigate('HomePage', { testParam: 'Test' })

Or you can also Dispatch Navigation Actions for this purpose as follow: 或者,您也可以为此目的调度导航操作,如下所示:

import { NavigationActions } from 'react-navigation';

const navigateAction = NavigationActions.navigate({ 

     routeName: 'HomePage', 

     params: { testParam: 'Test' }
});

this.props.navigation.dispatch(navigateAction);

I hope it help you. 希望对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM