0

I am new to react native and I am trying to create a menu, that would open on click and slide out, and on click outside the menu would slide back in.

It has been very hard for me to find any decent tutorial/explanation about how to have both stack and drawer navigation available for a page and functioning.

currently, my App.js looks like this:

    import 'react-native-gesture-handler';
import React from 'react';
import Home from './app/screens/Home/Home';
import ArtistListing from './app/screens/ArtistListing/ArtistListing';
import ArtPeriods from './app/screens/ArtPeriods/ArtPeriods';
import Login from './app/screens/Login/Login';
import Quiz from './app/screens/Quiz/Quiz';
import GuessWhen from './app/screens/GuessWhen/GuessWhen';
import { NavigationContainer, useLinking } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
import {Auth, Hub} from 'aws-amplify';
import {SetCurrentUser} from './src/model/User';
import LearnMore from './app/screens/LearnMore/LearnMore';
import {CreateAllArtistsWithDependencies} from './src/model/Artists';
import ExploreTimePeriod from './app/screens/ExploreTimePeriod/ExploreTimePeriod';
import ExploreArtist from './app/screens/ExploreArtist/ExploreArtist';
import PhotoGalleryScreen from './app/screens/PhotoGalleryScreen/PhotoGalleryScreen';
import ExploreTimePeriods from './app/screens/ExploreTimePeriods/ExploreTimePeriods';
import ExploreArtists from './app/screens/ExploreArtists/ExploreArtists';
import QuizSummary from './app/screens/QuizSummary/QuizSummary';
import ContactUs from './app/screens/ContactUs/ContactUs';
import Profile from './app/screens/Profile/Profile';
import Favorites from './app/screens/Favorites/Favorites';
const Stack = createStackNavigator();
const App = () => {
  return (
      <NavigationContainer>
          <Stack.Navigator initialRouteName="Login">
              <Stack.Screen name="Home" component={Home} options={{headerShown:false}} />
              <Stack.Screen name="Login" component={Login} options={{headerShown:false}} />
              <Stack.Screen name="ArtistListing" component={ArtistListing} options={{headerShown:false}}/>
              <Stack.Screen name="ArtPeriods" component={ArtPeriods} options={{headerShown:false}}/>
              <Stack.Screen name="GuessWhen" component={GuessWhen} options={{headerShown:false}}/>
              <Stack.Screen name="Quiz" component={Quiz} options={{headerShown:false}}/>
              <Stack.Screen name="LearnMore" component={LearnMore} options={{headerShown:false}}/>
              <Stack.Screen name="ExploreTimePeriod" component={ExploreTimePeriod} options={{headerShown:false}}/>
              <Stack.Screen name="ExploreTimePeriods" component={ExploreTimePeriods} options={{headerShown:false}}/>
              <Stack.Screen name="PhotoGalleryScreen" component={PhotoGalleryScreen} options={{headerShown:false}}/>
              <Stack.Screen name="ExploreArtist" component={ExploreArtist} options={{headerShown:false}}/>
              <Stack.Screen name="ExploreArtists" component={ExploreArtists} options={{headerShown:false}}/>
              <Stack.Screen name="QuizSummary" component={QuizSummary} options={{headerShown:false}}/>
              <Stack.Screen name="ContactUs" component={ContactUs} options={{headerShown:false}}/>
              <Stack.Screen name="Profile" component={Profile} options={{headerShown:false}}/>
              <Stack.Screen name="Favorites" component={Favorites} options={{headerShown:false}}/>
          </Stack.Navigator>
      </NavigationContainer>
  );
};

export default App;

I would like to have a drawer navigator as well with the pages listed below. I know I might need a switch navigator, but everything is super hard to find for version 5. I bet I am not the only one searching for a clear answer on how to do this.

          <Drawer.Screen name="ContactUs" component={ContactUs} options={{headerShown:false}}/>
      <Drawer.Screen name="Profile" component={Profile} options={{headerShown:false}}/>
      <Drawer.Screen name="Favorites" component={Favorites} options={{headerShown:false}}/> 

If you have an idea about this, please give me a suggestion.

Nata Vacheishvili
  • 387
  • 1
  • 5
  • 18
  • 1
    Do you want drawer navigation there on every screen? – Shahanshah Alam Jun 01 '20 at 20:08
  • @ShanAlam yes user should be able to click on the menu item and get the menu items on each of these screens here (Login page will not have a header so they simply won't be able to click and navigate) – Nata Vacheishvili Jun 01 '20 at 20:10
  • Basically you want an AuthFlowStack i.e. before login, and after login ContentStack with drawer navigation. right? – Shahanshah Alam Jun 01 '20 at 20:13
  • And react navigation 5 doesn't have SwitchNavigator that is why you are having issue – Shahanshah Alam Jun 01 '20 at 20:14
  • I have not worked with SwitchNavigator before ever - I am really new to react. I did work on all these screens but still do not have a proper menu. I'd like to have an auth stack and after auth stack, switch to stack navigator and be able to access drawer navigator on every screen of the stack. Not sure if you know how to do this and if you can walk me through an easy example, but my main priority is not authstack right now. If I can learn about it great, but otherwise I really want to know how to be able to access main menu with some of the screens that I have defined in the stack navigation – Nata Vacheishvili Jun 01 '20 at 20:17
  • Add your drawer navigator to stack screen and you can have drawer navigation on any screen, wherever you want. – Shahanshah Alam Jun 01 '20 at 20:30
  • @ShanAlam thanks a lot, but can you please give me an example more specifically ? It does not have to be an exact solution to my problem, but would you please be able to give me an example with code? do I have to nest somehow drawer navigation? This is the first time ever that I will be creating any kind of navigation for an app. You can answer my question with that if you'd like and I would accept it if it works out – Nata Vacheishvili Jun 01 '20 at 20:33

1 Answers1

1

Let'say your stack after login like

const Stack = createStackNavigator();
const App = () => {
return (
  <NavigationContainer>
  <Stack.Navigator initialRouteName="Home">
  <Stack.Screen name="Home" component={Home} options={{headerShown:false}} />
  <Stack.Screen name="ArtistListing" component={ArtistListing} options={{headerShown:false}}/>  
  </Stack.Navigator>
  </NavigationContainer>
  );
  };

Let's say this is your drawer navigation.

const Drawer = createDrawerNavigator();
function drawers(){
<Drawer.Screen name="Home" component={App} options={{headerShown:false}}/>
 <Drawer.Screen name="ContactUs" component={ContactUs} options={{headerShown:false}}/>
  <Drawer.Screen name="Profile" component={Profile} options={{headerShown:false}}/> 
   }

Now Drawer will be everywhere by default. And HomeScreen will be your first screen by default.

Now the part of AuthFlow.

const AuthFlowStack = createStackNavigator();
function AuthFlow(){
<AuthFlow.Screen name='LogIn' component={LogIn} />
}

Main Stack Flow(which will act as SwitchNavigator)

const Main = createStackNavigator();
function MainFlow(){
{this.state.isLogin ? <Drawer/> :<AuthFlow/>}
}
export default {MainFlow};

And set the value of isLogin to true when you login and send as parameter and set it false when logout.

Shahanshah Alam
  • 565
  • 7
  • 22
  • This is great and I understand what you mean here and its amazing example of the auth flow honestly to say, because everything that I have seen so far seemed very complicated. now I am wondering, do I need to create the slide out and in action on menu click myself and control the state of the menu being open and closed myself as well? – Nata Vacheishvili Jun 01 '20 at 21:17
  • as I saw I can have drawerContent as an attribute and give it a component. Would that work on sliding out and in itself / – Nata Vacheishvili Jun 01 '20 at 21:18
  • what you recommended works just fine, I am able to access the pages now from navigator - if there is any shortcut to opening and closing the drawer and how to control what data is passed to it ? – Nata Vacheishvili Jun 01 '20 at 21:20
  • Yes you have to do like this. But if it doesn't work then change the flow put AppStack in DrawerStack and add drawerStack to MainStack instead of AppStack. – Shahanshah Alam Jun 01 '20 at 21:20
  • so I could give it a drawerContent and it should control itself opening or not? – Nata Vacheishvili Jun 01 '20 at 21:22
  • may be it can control itself. I have not try running the code but this is general idea. – Shahanshah Alam Jun 01 '20 at 21:24
  • ok I think my problem is that I cannot open a drawer because it is considered as stack.screen – Nata Vacheishvili Jun 01 '20 at 21:25
  • if you are facing this issue then change the flow as I recommended in above comment. – Shahanshah Alam Jun 01 '20 at 21:27
  • Thank you so much for all your help!! Much appreciated - you are helping me understand something I have never worked with right now! – Nata Vacheishvili Jun 01 '20 at 21:31
  • I do have some bugs, but your explanation definitely led me to something that is working at least. so the menu is opening up, but the items I see in it are Stack and Drawer, but I can figure out the rest. once I do - I will make sure to post it here. Thanks a lot for everything ! – Nata Vacheishvili Jun 01 '20 at 21:41