在React导航5中使用抽屉式导航传递数据/参数

时间:2020-08-25 02:02:14

标签: reactjs react-native react-navigation

我在属于不同导航器的2个屏幕组件之间传递参数时遇到问题。

首先,我在Stack导航器中具有Login组件,然后在Drawer导航器中具有Home组件。 Hirarchy就像这样:

好吧,我将在“登录名”和“主页”中显示我的完整代码:

(登录组件)

import React, {useState} from 'react';
import {StyleSheet, Text, View, SafeAreaView} from 'react-native';
import {Button, TouchableRipple} from 'react-native-paper';
import {
  widthPercentageToDP as wp,
  heightPercentageToDP as hp,
} from 'react-native-responsive-screen';

import {Container, baseColor} from '../Styles';

const users = [
  {
    id: 72780,
    name: 'Sample 1',
    login: 'videouser1',
    password: 'videouser1',
    color: '#34ad86',
  },
  {
    id: 72781,
    name: 'Sample 2',
    login: 'videouser2',
    password: 'videouser2',
    color: baseColor,
  },
];
const Login = ({navigation}) => {
  const toHome = (user) => {
    navigation.navigate('Drawer', {screen: 'Home', params: {user}});
  };

  return (
    <View style={Container}>
      <Button
        mode="contained"
        uppercase={false}
        onPress={() => toHome(users[0])}
        style={styles.button}>
        Login as Sample 1
      </Button>
      <Button
        mode="contained"
        uppercase={false}
        onPress={() => toHome(users[1])}
        style={styles.button}>
        Login as Sample 2
      </Button>
    </View>
  );
};

export default Login;

const styles = StyleSheet.create({
  button: {
    margin: hp('2%'),
  },
});

登录组件位于Stack Navigator中:

(堆栈导航器)

import * as React from 'react';
import {createStackNavigator} from '@react-navigation/stack';

import SplashScreen from '../Screen/SplashScreen';
import Login from '../Screen/Login';
import DrawerNavigator from './DrawerNavigator';

const Stack = createStackNavigator();

function StackNavigator() {
  return (
    <Stack.Navigator>
      <Stack.Screen
        name="SplashScreen"
        component={SplashScreen}
        options={{headerShown: false}}
      />
      <Stack.Screen
        name="Login"
        component={Login}
        options={{headerShown: false, gestureEnabled: false}}
      />
      <Stack.Screen
        name="Drawer"
        component={DrawerNavigator}
        options={{headerShown: false, gestureEnabled: false}}
      />
    </Stack.Navigator>
  );
}

export default StackNavigator;

我在Drawer Navigator中有Home组件:

import * as React from 'react';
import {createDrawerNavigator} from '@react-navigation/drawer';

import Home from '../Screen/Home';
import Profile from '../Screen/Profile';
import Settings from '../Screen/Settings';
import CustomDrawerContent from './CustomDrawerContent';

const Drawer = createDrawerNavigator();

function DrawerNavigator({route, navigation}) {
  console.log('params', route.params);
  return (
    <Drawer.Navigator
      initialRouteName="Home"
      drawerContent={(props) => <CustomDrawerContent {...props} />}>
      <Drawer.Screen name="Home" component={BottomTabs} />
      <Drawer.Screen name="Profile" component={Profile} />
      <Drawer.Screen name="Settings" component={Settings} />
    </Drawer.Navigator>
  );
}

export default DrawerNavigator;

最后,这是我的Home组件:

import React from 'react';
import {StyleSheet, Text, View} from 'react-native';

const Home = ({route, navigation}) => {
  console.log('user', route);
  return (
    <View>
      <Text>Home</Text>
    </View>
  );
};

export default Home;

const styles = StyleSheet.create({});

我必须将参数从Login传递到Home组件,该怎么办?我已经按照React Navigation 5文档进行操作,但是运行不正常。

enter image description here

1 个答案:

答案 0 :(得分:1)

尝试一下:

import * as React from 'react';
import {createDrawerNavigator} from '@react-navigation/drawer';
    
import Home from '../Screen/Home';
import Profile from '../Screen/Profile';
import Settings from '../Screen/Settings';
import CustomDrawerContent from './CustomDrawerContent';

const Drawer = createDrawerNavigator();

function DrawerNavigator({route, navigation}) {
  console.log('params', route.params);
  return (
    <Drawer.Navigator
      initialRouteName="Home"
      drawerContent={(props) => <CustomDrawerContent {...props} />}>
      <Drawer.Screen name="Home" initialParams={{ params: route.params }} component={BottomTabs} />
      <Drawer.Screen name="Profile" component={Profile} />
      <Drawer.Screen name="Settings" component={Settings} />
    </Drawer.Navigator>
  );
}

export default DrawerNavigator;