TypeError: undefined не является объектом, полученным при использовании navigation.openDrawer () - PullRequest
0 голосов
/ 28 апреля 2020

При попытке открыть компонент sidedrawer с помощью navigation.openDrawer() я получаю эту ошибку. Я попытался вложить компонент выдвижного ящика в NavigationCOntainer, который не сработал. Ожидается, что боковой ящик выдвинется при нажатии на значок

HomeStack. js

import 'react-native-gesture-handler';
import React from 'react';
import {NavigationContainer} from '@react-navigation/native';
import {createStackNavigator} from '@react-navigation/stack';
import Login from '../login/login';
import Register from '../register/register';
import Welcome from '../welcome/welcome';
import ResetPWD from '../register/resetpwd';
import RegisterLogin from '../register/registerlogin';
import About from '../screens/about';
import More from '../screens/more';
import Gallery from '../screens/gallery';
import Excos from '../screens/excos';
import AbuladWebsite from '../screens/abuladwebsite';
import LifeInAbu from '../screens/lifeinabu';
import {Icon} from 'react-native-elements';
// import SideStack from '../routes/sideStack';
import {createDrawerNavigator} from '@react-navigation/drawer';

const Stack = createStackNavigator();
const Drawer = createDrawerNavigator();

function sideDrawer({navigation}) {
  return (
    <Drawer.Navigator>
      <Drawer.Screen name="Gallery" component={Gallery} />
      <Drawer.Screen name="Life in ABU" component={LifeInAbu} />
      <Drawer.Screen name="Buy and Sell" component={AbuladWebsite} />
      <Drawer.Screen name="About" component={About} />
      <Drawer.Screen name="Excos" component={Excos} />
      <Drawer.Screen name="More" component={More} />
    </Drawer.Navigator>
  );
}

function homeStack({navigation}) {
  return (
    <NavigationContainer>
      <Stack.Navigator>
        <Stack.Screen
          name="Login"
          component={Login}
          options={{headerShown: false}}
        />
        <Stack.Screen
          name="Register"
          component={Register}
          options={{headerShown: false}}
        />
        <Stack.Screen
          name="ResetPWD"
          component={ResetPWD}
          options={{headerShown: false}}
        />
        <Stack.Screen
          name="RegisterLogin"
          component={RegisterLogin}
          options={{headerShown: false}}
        />
        <Stack.Screen name="sideStack" component={sideDrawer} />
        <Stack.Screen
          name="Welcome"
          component={Welcome}
          options={{
            title: 'Abulad',
            headerLeft: () => (
              <Icon
                name="android"
                onPress={() => navigation.openDrawer()}
                // title="Info"
                color="#000"
              />
            ),
          }}
        />
      </Stack.Navigator>
    </NavigationContainer>
  );
}

export default homeStack;

Приложение. js


import React, {Component} from 'react';
import SplashScreen from 'react-native-splash-screen';
import HomeStack from './src/components/routes/homeStack';
import SideStack from './src/components/routes/sideStack';

class App extends Component {
  componentDidMount() {
    SplashScreen.hide();
  }
  render() {
    return <HomeStack />;
  }
}

export default App;


error image

1 Ответ

1 голос
/ 28 апреля 2020

Чтобы использовать openDrawer (), вам нужно обернуть свой стековый навигатор в навигатор ящиков, вы можете сделать что-то вроде этого:

    import 'react-native-gesture-handler';
    import React from 'react';
    import {NavigationContainer} from '@react-navigation/native';
    import {createStackNavigator} from '@react-navigation/stack';
    import Login from '../login/login';
    import Register from '../register/register';
    import Welcome from '../welcome/welcome';
    import ResetPWD from '../register/resetpwd';
    import RegisterLogin from '../register/registerlogin';
    import About from '../screens/about';
    import More from '../screens/more';
    import Gallery from '../screens/gallery';
    import Excos from '../screens/excos';
    import AbuladWebsite from '../screens/abuladwebsite';
    import LifeInAbu from '../screens/lifeinabu';
    import {Icon} from 'react-native-elements';
    // import SideStack from '../routes/sideStack';
    import {createDrawerNavigator} from '@react-navigation/drawer';

    const Stack = createStackNavigator();
    const Drawer = createDrawerNavigator();

    function sideDrawer({navigation}) {
      return (
        <NavigationContainer>
        <Drawer.Navigator>
            <Drawer.Screen name="Home" component={homeStack} />
          <Drawer.Screen name="Gallery" component={Gallery} />
          <Drawer.Screen name="Life in ABU" component={LifeInAbu} />
          <Drawer.Screen name="Buy and Sell" component={AbuladWebsite} />
          <Drawer.Screen name="About" component={About} />
          <Drawer.Screen name="Excos" component={Excos} />
          <Drawer.Screen name="More" component={More} />
        </Drawer.Navigator>
        </NavigationContainer>
      );
    }

    function homeStack({navigation}) {
      return (
          <Stack.Navigator>
             <Stack.Navigator>
    <Stack.Screen
      name="Welcome"
      component={Welcome}
      options={{
        title: 'Abulad',
        headerLeft: () => (
          <Icon
            name="android"
            onPress={() => navigation.openDrawer()}
            // title="Info"
            color="#000"
          />
           ),
          }}
          />
            <Stack.Screen
              name="Login"
              component={Login}
              options={{headerShown: false}}
            />
            <Stack.Screen
              name="Register"
              component={Register}
              options={{headerShown: false}}
            />
            <Stack.Screen
              name="ResetPWD"
              component={ResetPWD}
              options={{headerShown: false}}
            />
            <Stack.Screen
              name="RegisterLogin"
              component={RegisterLogin}
              options={{headerShown: false}}
            />
            <Stack.Screen name="sideStack" component={sideDrawer} />

          </Stack.Navigator>
      );
    }

    export default sideDrawer;

enter image description here

enter image description here

Надеюсь, это поможет!

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...