如何将prop传递给createBottomTabNavigator的所有选项卡

问题描述 投票:2回答:2

我的应用程序使用以下createBottomTabNavigatorcreateSwitchNavigator调用this.props.navigation.navigate('AppMenu', {UserName: 'First Name'});.createBottomTabNavigator由2个选项卡组成,一个是Stack Navigator,第二个是React组件。当我导航到Stack Navigator的initialRouteName时,我传入的道具可用,但是如果我选择Tab Navigator的第二个标签,则道具不可用。

有没有一种方法可以在渲染底部选项卡导航器的所有选项卡时将道具传递给它?

下面是可以在Snack中运行的应用程序和示例代码的图表。

enter image description here

import React from 'react';
import { Text, View, Button } from 'react-native';
import { createBottomTabNavigator, createStackNavigator, createSwitchNavigator } from 'react-navigation';

class LoginScreen extends React.Component {
  _navToMain = () => {
    console.log('Login Screen: Passing user: First Name');
    this.props.navigation.navigate('AppMenu', {UserName: 'First Name'});
  }
  render () {
    return (
      <View style={{flexDirection: 'column', paddingTop:20}}>
        <Text>Pass UserName prop to createBottomTabNavigator</Text>
        <Button title='Login!' onPress={this._navToMain}/> 
      </View>
    )
  }
}

class SyncScreen extends React.Component {
  static navigationOptions = ({ navigation }) => {
    return {
      headerTitle: 'Sync Screen',
    };
  };
  render () {
    return (  
      <View style={{flexDirection: 'column', paddingTop:20}}>
        <Text>Sync Screen Navigation Prop UserName (missing!): {JSON.stringify(this.props.navigation.getParam('UserName'))}</Text>          
        <Button title='Test Button' onPress={() => {}} />
      </View>
    );
  }
}

class AppMenuScreen extends React.Component {
  static navigationOptions = ({ navigation }) => {
    return {
      headerTitle: 'Stack Navigator',
    };
  };

  _navToOne = () => {
    console.log('Going to Stack One')
    this.props.navigation.navigate('StackOne', {UserName: this.props.navigation.getParam('UserName')});
  }

  render () {
    return (   
      <View style={{flexDirection: 'column'}} >  
        <Text>App Menu Navigation Prop UserName passed from SwitchNavigator: {JSON.stringify(this.props.navigation.getParam('UserName'))}</Text>
        <Button title='Screen one' onPress={this._navToOne} />         
      </View> 
    )
  }
}

class StackOneScreen extends React.Component {
  static navigationOptions = ({ navigation }) => {
    return {
      headerTitle: 'Stack One Screen',
    };
  };
  render () {
    return (         
      <View style={{flexDirection: 'row'}} >  
        <Text>Stack One Screen Navigation Props: {JSON.stringify(this.props.navigation.getParam('UserName'))}</Text>       
      </View> 
    )
  }
}

const AppStack = createStackNavigator(
  {
    AppMenu: AppMenuScreen,
    StackOne: StackOneScreen,    
  },
  {
    initialRouteName: "AppMenu",
    navigationOptions: {
      headerTintColor: "#a41034",
      headerStyle: {
        backgroundColor: "#fff"
      }
    }
  }
);

const MainTabs = createBottomTabNavigator(
  {
    'Apps': { screen: AppStack },
    'Sync': { screen: SyncScreen },
  },
  {
    navigationOptions: ({ navigation }) => ({
      title: navigation.state,      
      tabBarIcon: ({ focused, horizontal, tintColor }) => {
        const { routeName } = navigation.state;
      }
    }),
    tabBarOptions: {
      activeTintColor: 'red',
      inactiveTintColor: 'gray',
      tabBackgroundColor: 'black',      
      labelStyle:{
        fontSize: 24,
        marginBottom:10,
      }       
    },
    animationEnabled: true,
    swipeEnabled: false,
  }
);

const AppNavigator = createSwitchNavigator(
  {
    Login: LoginScreen,
    Main: MainTabs
  },
  {
    initialRouteName: 'Login',
    backBehavior: "initialRoute",
  }
);

export default class App extends React.Component {
  render() {
    return (
      <AppNavigator />
    );
  }
}

的package.json

"react": "16.5.0",
"react-native": "0.57.1",    
"react-navigation": "^2.0.0"
react-native react-navigation
2个回答
0
投票

这只是一个临时解决方案,至少这些代码可以解决您的问题


当我导航到Stack Navigator的initialRouteName时,我传入的prop可用,但是如果我选择Tab Navigator的第二个选项卡,则prop不可用

因为你刚刚使用AppMenu将参数发送到this.props.navigation.navigate('AppMenu', {UserName: 'First Name'});

更改UserName类的代码后,LoginScreen在两个选项卡上都可用:

class LoginScreen extends React.Component {
  _navToMain = () => {
    console.log('Login Screen: Passing user: First Name');
    //-------add this-------
    this.props.navigation.navigate('Sync', {UserName: 'First Name'});
    //----------------------
    this.props.navigation.navigate('AppMenu', {UserName: 'First Name'});
  }
  render () {
    return (
      <View style={{flexDirection: 'column', paddingTop:20}}>
        <Text>Pass UserName prop to createBottomTabNavigator</Text>
        <Button title='Login!' onPress={this._navToMain}/> 
      </View>
    )
  }
}

0
投票

你可以使用导航来传递道具

 _navToOne = () => {
  console.log("Going to Stack One");
  this.props.navigation.navigate({
    routeName: "StackOne",
    params: { UserName: this.props.navigation.state.params.UserName }
  });
};

如果你想访问使用导航传递的道具

this.props.navigation.state.params.PROPNAME
// PROPNAME is the prop you passed and want to get
© www.soinside.com 2019 - 2024. All rights reserved.