Nesting Stack Navigator with Drawer Navigator - navigation-drawer

I am trying to have stack navigation along with drawer navigation. Basically I want the drawer to be in only one scene all time. I have tried to do so but nothing occurs. I am not being able to understand what I am doing wrong.
P.S: I am new to react-native.
ConversationListScreen.js
import React, { Component } from 'react';
import {
View,
Text,
Button,
TouchableNativeFeedback,
Alert,
} from 'react-native';
import Icon from 'react-native-vector-icons/MaterialIcons';
import styles from './styles';
export default class ConversationListScreen extends Component
{
static navigationOptions = {
title: 'Hola',
headerLeft: (
<TouchableNativeFeedback onPress={() => Alert.alert('Hi!', 'I am a hamburger.')}>
<View style={styles.toolBackground}>
<Icon name="menu" style={ styles.menuIcon }/>
</View>
</TouchableNativeFeedback>
),
headerRight: (
<TouchableNativeFeedback onPress={() => Alert.alert('Hi!', 'What you want to search?')}>
<View style={styles.toolBackground}>
<Icon name="search" style={ styles.searchIcon }/>
</View>
</TouchableNativeFeedback>
)
};
render() {
return (
<View>
</View>
);
}
}
Drawer.js
import React, { Component } from 'react';
import {
View,
Text,
} from 'react-native';
export default class SideNav extends Component
{
render() {
return (
<View>
<Text>My first Drawer</Text>
</View>
);
}
}
RouteConfig.js
import { StackNavigator, DrawerNavigator } from 'react-navigation';
import ConversationListScreen from './ConversationListScreen';
import Drawer from './Drawer';
import ChatScreen from './ChatScreen';
export const SideNav = DrawerNavigator({
Drawer: { screen: Drawer},
});
export const Hola = StackNavigator({
ConversationList: { screen: ConversationListScreen },
Drawer: { screen: SideNav },
Chat: { screen: ChatScreen },
});

I had a similar issue and what I ended up doing was using this drawer component and wrapping it around the component you want to access it with. For example if you want to access it only from ConversationListScreen you export it as <Drawer><ConversationListScreen></Drawer>

I guess you can't use drawer navigator in stack navigator as a scene. you should import SideNav in the component you want to use and try to render it as a component

Here is a workaround. We have two screens in HomeScreenNavigator which is a stackNavigator. in firstScreen we have a link to secondScreen. in secondScreen we have drawerNavigator which has a screen named ComponentWidthDrawer. this component has a button to open drawer. I tested it and it's working.
class FirstComponent extends Component{
render () {
return (
<View>
<TouchableOpacity onPress={() =>
this.props.navigation.navigate("SecondScreen")}>
<Text>Go to Second Component</Text>
</TouchableOpacity>
</View>
)
}
}
class ComponentWidthDrawer extends Component{
render () {
return (
<View>
<TouchableOpacity onPress={() => this.props.navigation.navigate("DrawerOpen")}>
<Text>Open Menu</Text>
</TouchableOpacity>
</View>
)
}
}
const SecondComponent = DrawerNavigator({
Drawer: {
screen: ComponentWidthDrawer,
navigationOptions: {
drawerLabel: 'Videos'
},
}
})
const HomeScreenNavigator = StackNavigator(
{
FirstScreen : {
screen: FirstComponent,
navigationOptions:{
header: false
}
},
SecondScreen: {
screen: SecondComponent,
navigationOptions:{
header: false
}
}
}
);

Related

React native Drawer only after login

I have snack link here
https://snack.expo.io/#mparvez19861/drawer-navigation?session_id=snack-session-hyLuO4xPa
I am trying to show drawer only if user login, for login there will not be any drawer.
Please help
Thanks
You can accomplish this by wrapping your Drawer Navigator in a Switch Navigator. Upon logging in, the Switch Navigator will switch from the login screen (which is just a screen), to the main App screen (which is initialized with createDrawerNavigator).
import React, { Component } from 'react';
import { View, Text, TouchableOpacity, StyleSheet } from 'react-native';
import {
createSwitchNavigator,
createStackNavigator,
createAppContainer,
createDrawerNavigator,
} from 'react-navigation';
class Screen extends Component {
render() {
return (
<View style={styles.container}>
<Text>Screen</Text>
</View>
);
}
}
class AuthScreen extends Component {
render() {
return (
<View style={styles.container}>
<Text>Auth Screen</Text>
<TouchableOpacity onPress={() => this.props.navigation.navigate('App')}>
<Text>Login</Text>
</TouchableOpacity>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
marginTop: 40,
justifyContent: 'center',
alignItems: 'center',
},
});
const SomeStackNavigator = createStackNavigator({
ScreenA: Screen,
ScreenB: Screen,
});
const AppStack = createDrawerNavigator({
StackA: {
name: 'StackA',
screen: SomeStackNavigator,
},
StackB: {
name: 'StackB',
screen: SomeStackNavigator,
},
});
const AppNavigator = createSwitchNavigator(
{
App: AppStack,
Auth: {
screen: AuthScreen,
},
},
{
initialRouteName: 'Auth',
},
);
const AppContainer = createAppContainer(AppNavigator);
export default AppContainer;
Check it out here.

Having issue to create navigation between screen

I have a splash screen which goes to the Intro page after a few seconds and from the Intro page, I have a link for sign in and sign up. I tried to set up navigation from the Intro page to/from Login but encounter an error which says:
undefined is not an object (evaluating '_this.props.navigation.navigate').
I have tried to check the documentation on react-navigation but not clear with the explanation as they muddled up all screens in App.js.
App.js
import React, {Component} from 'react';
import { StyleSheet, StatusBar, Text, View} from 'react-native';
import { createStackNavigator, createAppContainer } from 'react-navigation';
import Intro from './screen/Intro';
import Login from './components/Login';
const RootStack = createStackNavigator(
{
IntroScreen: Intro,
LoginScreen: Login,
},
{
initialRouteName: 'IntroScreen',
}
);
const AppNavigator = createAppContainer(RootStack);
export default class App extends Component {
render() {
return (
<View style={styles.container}>
<StatusBar
barStyle="light-content"
backgroundColor="#fefefe"
/>
<Intro/>
</View>
);
}
}
Intro.js
The page follows the splash screen display and from the page I can select login or sign up.
import React, { Component } from 'react';
import {
Text,
Image,
View,
ImageBackground,
StyleSheet,
TouchableOpacity
}
from 'react-native';
import SplashScreen from 'react-native-splash-screen';
export default class Intro extends Component{
componentDidMount(){
SplashScreen.hide();
}
render(){
return(
<ImageBackground source={require('../images/signup-background.jpg')} style={{width: '100%', height: '100%'}}>
<View style={styles.container}>
<Image
source ={ require('../images/logo.png')}
/>
<Text style={styles.simpleText}>WELCOME</Text>
<Text style={styles.literal}>Thank you for your interest in the APG app. </Text>
<Text style={styles.literal}>How can we help you?</Text>
<TouchableOpacity
style= {styles.signinCont}
onPress={() => this.props.navigation.navigate('IntroScreen')}
>
<Text style= {styles.signinText}>
I am already an APG Patient
</Text>
</TouchableOpacity>
<TouchableOpacity style= {styles.signupCont}>
<Text style= {styles.signupText}>
I am not an APG Patient
</Text>
</TouchableOpacity>
</View>
</ImageBackground>
);
}
}
Login.js
On the Login, I have a button to link to Signup and forgot password in case the user doesn't have an account
import React, { Component } from 'react';
import {
Text,
TouchableOpacity,
Label,
View,
TextInput,
StyleSheet,
}
from 'react-native';
export default class Login extends Component {
render(){
return(
<View style={styles.container}>
<View>
<Text>Login</Text>
</View>
<Label>Email</Label>
<TextInput
style={Styles.textbox}
placeholder="Email"
value= {this.state.email}
/>
<Label>Password</Label>
<TextInput
style={Styles.textbox}
placeholder="Password"
value ={this.state.password}
secureTextEntry={true}
/>
<View>
<Text>Forgot password</Text>
</View>
<TouchableOpacity style={styles.signin}>
<Text style ={styles.signinText}>Sign In</Text>
</TouchableOpacity>
<View>
<Text>Not an APG Member? </Text>
<TouchableOpacity style={styles.signup}
onPress ={() => this.props.navigation.navigate('SignUp') }
>
<Text>Sign Up</Text>
</TouchableOpacity>
</View>
</View>
);
}
}
I want to be able to choose either the login or signup as the case may be from the Intro.js and on login.js to be able to click on signup for registration and from signup to be able to click login.
Solution
onPress={this.props.navigation.navigate('SignUp')} do not invoke this.props.navigation.navigate function.
Change your onPress property as below.
Use es6 arrow function for making anonymous function.
<TouchableOpacity
style={styles.signup}
onPress={() => this.props.navigation.navigate('SignUp')}
>
or you can reference your function in onPress. It makes performance better when render again.
navigate = () => {
this.props.navigation.navigate('SignUp')
}
return (
...
<TouchableOpacity
style={styles.signup}
onPress={this.navigate}
>
)
Why
props only references value even if it is function. So make props reference anonymous function or funtion in class you predefined.
And do not forget bind(this) when you use not arrow function.
navigate() {
this.props.navigation.navigate('SignUp')
}
...
onPress={this.navigate.bind(this)}
...

the image of the openSelectDialog method of the ImagePickerIOS in the React Native is not output

Here is my code:
import React, {Component} from 'react';
import {ImagePickerIOS, Image, Text, View} from 'react-native';
class Dashboard extends Component {
constructor(props) {
super(props);
this.state = {
image: ''
};
}
componentDidMount() {
ImagePickerIOS.openSelectDialog({}, imageUri => {
this.setState({ image: imageUri });
}, error => console.log(error));
}
render() {
return (
<View>
<Text>{JSON.stringify(this.state.image)}</Text>
{ this.state.image ?
<Image style={{flex: 1}} source={{uri: this.state.image}} />
: null }
</View>
);
}
}
export default Dashboard
When you run the above code, this.state.image will have "assets-library: //asset/asset.HEIC? Id = .... & ext = HEIC".
However, is not output.
And <Image style={{width: 200, height: 200}} source={{uri: this.state.image}} />
This style property puts the IOS simulator to a black screen and exits the app.
The React Native version is 0.57.4 and the simulator is iPhone X - 12.1.
Help.
I have the same problem. But I have solved this issue.
You may need add width and height to style.
{ this.state.image ? <Image style={{height: 100, width: 100}} source={{uri: this.state.image}} /> : null }

Changing screen gives undefined props.navigation error react native

I am trying to change screen after few seconds which is for splash screen effect in react native. I have a main screen in which app originates. The code for first screen is:
import HomeUp from './HomeUp'
import Splash from './Splash'
import React, { Component } from "react";
export default class OriginPage extends Component {
constructor(props){
super(props)
this.state = {
component : <Splash />
}
}
componentDidMount(){
// Start counting when the page is loaded
this.timeoutHandle = setTimeout(()=>{
// Add your logic for the transition
this.setState({ component: <HomeUp /> })
}, 5000);
}
componentWillUnmount(){
clearTimeout(this.timeoutHandle);
}
render() {
return (
this.state.component
);
}
}
My splash screen is :
import React from 'react';
import { StatusBar , View , Text , ActivityIndicator } from 'react-native';
export default class Splash extends React.Component {
render() {
return (
<View style={{ flex: 1 , justifyContent: 'center' , alignItems: 'center' , backgroundColor : '#34495e'}}>
<StatusBar backgroundColor="#2c3e50" barStyle="light-content"/>
<Text style={{ color : 'white',fontSize : 18 }}>Hello Splash</Text>
<ActivityIndicator color={'white'}/>
</View>
)
}
}
Till now everything works fine and the screen change over after 5s also working but this below code has bug.
import React, { Component } from "react";
class HomeUp extends Component {
render () {
const { navigate } = this.props.navigation;
return(
<TouchableHighlight onPress={() => navigate("Products", { product: item })} underlayColor="transparent">
<View style={styles.view} >
<Image style={styles.image} source={{uri: item.images[0].src}} />
<Text style={styles.name}>{item.name}</Text>
</View>
</TouchableHighlight>
);
}
}
Loading this screen alone have no bug but while changing screen I get undefined navigation.props error. hanks in adavanced.
If you are deconstructing your navigation props in your HomeUp component you should do it as follows:
const { navigation } = this.props;
Or also:
const navigate = this.props.navigation;
Hope it helps.

react-navigation - drawer navigation inside stack navigation in react native

Image of my codes, image of error
Managed to build my first React native-navigation v2, a stack navigation from LoginScreen to HomeScreen.
The problem I have is that I tried putting a Drawer Navigation on the HomeScreen and placing it inside the stack navigation but I can't understand the way to do it.
error message: Unable to resolve ./src/components/HomeScreen" from ".//src/components/DrawerNavigator.js: The module./src/components/HomeScreen` could not be found"
Failed building JavaScript bundle
Take a look at my code below.
LoginForm.js
import React, {Component} from 'react';
import {StyleSheet, View, TextInput, TouchableOpacity, Text, StatusBar} from 'react-native';
export default class LoginForm extends Component {
render() {
return (
<View style={styles.container}>
<StatusBar
barStyle="dark-content"
/>
<TouchableOpacity style={styles.buttonContainer} onPress={() => this.props.navigation.navigate('DrawerNavigator')}>
<Text style={styles.buttonText}>LOGIN</Text>
</TouchableOpacity>
</View>
);
}
}
App.js
import React from 'react';
import { StyleSheet, Text, View, ScrollView } from 'react-native';
import { Header, List, ListItem, Icon } from 'react-native-elements';
import Login from './src/components/Login/Login';
import LoginForm from './src/components/Login/LoginForm';
import HomeScreen from './src/components/HomeScreen';
import DayPlanScreen from './src/components/DayPlanScreen';
import { createStackNavigator } from 'react-navigation';
import DrawerNavigator from './src/components/DrawerNavigator'
export default class App extends React.Component {
static navigationOptions = {
header: null
}
render() {
return (
<View style={styles.container}>
{
<AppStackNavigator />
}
</View>
);
}
}
const AppStackNavigator = createStackNavigator ({
Login: Login,
Home: HomeScreen,
DrawerNavigator: DrawerNavigator
})
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'column',
backgroundColor: '#fff',
},
addBtn: {
position: 'absolute',
bottom: 10,
right: 0
},
});
DrawerNavigator.js
import React, { Component } from "react";
import {
View,
Text,
StyleSheet
} from "react-native";
import HomeScreen from './src/components/HomeScreen';
import { createDrawerNavigator } from 'react-navigation'
const DrawerNavigator = createDrawerNavigator({
Home: HomeScreen,
DayPlan: DayPlanScreen
})
export default DrawerNavigator;
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center'
}
});
HomeScreen
import React, { Component } from "react";
import {
View,
Text,
StyleSheet
} from "react-native";
import { Button } from 'react-native-elements';
class HomeScreen extends Component {
render(){
return (
<View style={styles.container}>
<Text style={{color: 'white'}}>HomeScreen</Text>
<Button
title='LOGIN SCREEN'
onPress={()=>this.props.navigation.goBack()}
fontWeight='700'
backgroundColor='#3AD7FF'
color='#424242'
/>
</View>
);
}
}
export default HomeScreen;
const styles = StyleSheet.create({
container:{
flex:1,
alignItems:'center',
justifyContent:'center',
backgroundColor: '#424242'
}
});

Resources