Hamburger icon refuses to show in react-native - react-native

So i started to play with Navigation in React-native and i am really having fun with it, Except i ran into deadlocks with using the navigation in a way. Now here is what the worry is.
I want the header displayed using the hamburger icon. So the user can click the Hamburger Icon and it shows the menu on the Left where the user can select and perform any task, i tried it and it never shows up.
My code looks thus :
App.js
import React , {Component} from 'react';
import { View, Text, Image , StyleSheet } from 'react-native';
import { createAppContainer } from 'react-navigation';
import { createDrawerNavigator } from 'react-navigation-drawer';
import HamburgerIconMain from './HamburgerIcon/hamburgerIconMain';
class Home extends React.Component{
static navigationOptions = () => {
return{
headerLeft:<HamburgerIconMain/>
};
};
static navigationOptions = {
drawerLabel: 'Home',
drawerIcon: ({ tintColor }) => (
<Image
source={{uri:'http://imageholder.freeasphost.net/home.png'}}
style={[styles.icon, { tintColor: tintColor }]}
/>
),
};
render()
{
return(
<View>
<Text> Welcome to Home screen</Text>
</View>
);
}
}
class Profile extends React.Component{
static navigationOptions = {
drawerLabel: 'Profile',
drawerIcon: ({ tintColor }) => (
<Image
source={{uri:'http://imageholder.freeasphost.net/profile.png'}}
style={[styles.icon, { tintColor: tintColor }]}
/>
),
};
render()
{
return(
<View>
<Text>Welcome to Profile screen</Text>
</View>
);
}
}
class Settings extends React.Component{
static navigationOptions = {
drawerLabel: 'Settings',
drawerIcon: ({ tintColor }) => (
<Image
source={{uri:'http://imageholder.freeasphost.net/settings.png'}}
style={[styles.icon, { tintColor: tintColor }]}
/>
),
};
render()
{
return(
<View>
<Text>Welcome to Settings Screen</Text>
</View>
);
}
}
const MyDrawerNavigator = createDrawerNavigator({
Home:{
screen:Home
},
Settings:{
screen:Settings
},
Profile:{
screen:Profile
},
});
const MyApp = createAppContainer(MyDrawerNavigator);
export default class App extends Component {
render() {
return (
<MyApp/>
);
}
}
const styles = StyleSheet.create({
icon: {
width: 24,
height: 24,
},
});
Then the code for the Hamburger icon looks like this
import React, {Component} from 'react';
import { withNavigation } from 'react-navigation';
import { TouchableOpacity } from "react-native-gesture-handler";
import Icon from 'react-native-vector-icons/SimpleLineIcons';
class HamburgerIconMain extends React.Component{
render(){
return(
<TouchableOpacity>
style={{
width: 44,
height: 44,
marginLeft: 20
}}
onPress={()=>{
this.props.navigation.openDrawer();
}}>
<Icon name='menu' size={20} color='black'/>
</TouchableOpacity>
);
}
}
export default withNavigation(HamburgerIconMain);
The worry I have now is , the header with the Hamburger Icon never shows up. How and what can i do to Resolve this

https://reactnavigation.org/docs/en/next/stack-navigator.html#docsNav
According to docs, you need to createStackNavigator.
Or, you can create your own header for Home screen with <HamburgerIconMain/> component.
Something like this.
render(){
return(
<View style={{flex:1, backgroundColor: 'green'}}>
<View style={{backgroundColor: 'yellow', height: 40, justifyContent: 'center'}}>
<Text> Header: add hamburger icon here </Text>
</View>
<View style={{flex: 1, backgroundColor: 'grey', justifyContent: 'center', alignItems: 'center'}}>
<Text> Welcome to Home screen</Text>
</View>
</View>
);
}
But such solution good just in case, if you want to see header just on one screen.

Related

How Can I add Hamburger icon menu in React-Native

I am fairly new to Navigation in react-native but i have been able to pick up few things here now i want o add the Hamburger icon to it to do a Proper Menu bar. Since i am fairly new to this, I need help in every way.
My code Looks something like this
import React , {Component} from 'react';
import { View, Text, Image , StyleSheet } from 'react-native';
import { createAppContainer } from 'react-navigation';
import { createDrawerNavigator } from 'react-navigation-drawer';
class Home extends React.Component{
static navigationOptions = {
drawerLabel: 'Home',
drawerIcon: ({ tintColor }) => (
<Image
source={{uri:'http://imageholder.freeasphost.net/home.png'}}
style={[styles.icon, { tintColor: tintColor }]}
/>
),
};
render()
{
return(
<View>
<Text> Welcome to Home screen</Text>
</View>
);
}
}
class Profile extends React.Component{
static navigationOptions = {
drawerLabel: 'Profile',
drawerIcon: ({ tintColor }) => (
<Image
source={{uri:'http://imageholder.freeasphost.net/profile.png'}}
style={[styles.icon, { tintColor: tintColor }]}
/>
),
};
render()
{
return(
<View>
<Text>Welcome to Profile screen</Text>
</View>
);
}
}
class Settings extends React.Component{
static navigationOptions = {
drawerLabel: 'Settings',
drawerIcon: ({ tintColor }) => (
<Image
source={{uri:'http://imageholder.freeasphost.net/settings.png'}}
style={[styles.icon, { tintColor: tintColor }]}
/>
),
};
render()
{
return(
<View>
<Text>Welcome to Settings Screen</Text>
</View>
);
}
}
const MyDrawerNavigator = createDrawerNavigator({
Home:{
screen:Home
},
Settings:{
screen:Settings
},
Profile:{
screen:Profile
},
});
const MyApp = createAppContainer(MyDrawerNavigator);
export default class App extends Component {
render() {
return (
<MyApp/>
);
}
}
const styles = StyleSheet.create({
icon: {
width: 24,
height: 24,
},
});
Like i wanted to use some drawer navigation uri to do it but how to start is the main worry, hence I wanted to know how and what i must do.
I'll always prefer making your own custom component rather than any library.
It's simple, first download icon like this or your icon and create a view box, and then on the image add touchable opacity so that when a user clicks, it will expand
will turn into this
Now steps are like that,
renderUnExpanded=()=>(
<View>
<TouchableOpacity onPress = {this.flipState} >
<Icon />
</TouchableOpacity>
<View>
)
renderExpanded = () =>(
<View>
<TouchableOpacity onPress = {this.flipState} >
<Icon />
</TouchableOpacity>
<ProfileIcon />
<SignoutIcon />
<View>
)
now flipState will be a function to toggle expanded state,
flipState =() => {
this.setState({optionVisible:!this.state.optionVisible})
}
and now in render function , show accordingly to state,
render(){
return(
{this.state.optionVisible?this.renderExpanded():this.renderUnExpanded()}
)
}
hope its clear, otherwise ask any doubts

Is it Possible to navigate from createStackNavigator to createDrawerNavigator?

How is this implemented
I have a stack navigator which i use for Splashscreen and Login, this works fine , now i have a drawerNavigator which is the main home of the Application, Now my worry is, Is this possible , navigating from a stack navigator (username and password) and landing at a homepage (DrawerNavigator) (home page with left side menu)
My code is looking something like this, its a very long code I know, but pls at the same time, I just started out react-native some few days ago. Does anyone think its advisable to use createStackNavigator as well as createDrawerNavigator at the same time?
import React , {Component} from 'react';
import { Platform, View, Text, Image , StyleSheet , ActivityIndicator, Dimensions, Modal, TextInput, TouchableOpacity, Alert } from 'react-native';
import { createAppContainer } from 'react-navigation';
import { createDrawerNavigator } from 'react-navigation-drawer';
import { Header } from 'react-native-elements';
import { Left, Right, Icon } from 'native-base';
import { createStackNavigator } from 'react-navigation-stack';
class SplashScreen extends React.Component{
componentDidMount()
{
setTimeout(() => {
this.props.navigation.navigate('Login')
}, 4000);
}
render(){
return(
<View style={styles.Logocontainer}>
<Image
source={{uri: 'LOGO IMAGE HERE'}}
style={styles.logo} />
<ActivityIndicator size="large" color="blue" style={{margin:10}}/>
</View>
);
}
}
class Login extends React.Component{
login(){
const {username, password} = this.state;
Alert.alert('Login Successful');
this.props.navigation.navigate('Home');
}
render(){
return(
<View style={styles.Logocontainer}>
<Image
source={{uri: 'LOGO IMAGE HERE'}}
style={styles.logo} />
<Text style={{textAlign:'left',fontSize:25,color: '#009999'}}> Sign In {"\n"}</Text>
<TextInput
onChangeText={(username) => this.setState({ username })}
placeholder = "Username"
style={styles.input}
/>
<TextInput
onChangeText={(password) => this.setState({ password })}
placeholder = "Password"
style={styles.input}
secureTextEntry={true} />
<TouchableOpacity style={styles.button} onPress={this.login.bind(this)}>
<Text style={styles.loginbtn}> Login </Text>
</TouchableOpacity>
</View>
);
}
}
class Home extends React.Component{
static navigationOptions = {
drawerLabel: 'Home',
drawerIcon: ({ tintColor }) => (
<Image
source={{uri:'http://imageholder.freeasphost.net/home.png'}}
style={[styles.icon, { tintColor: tintColor }]}
/>
),
};
render()
{
return(
<View style={styles.container}>
<Header
leftComponent={<Icon name="menu" onPress={() => this.props.navigation.openDrawer()} />}
/>
<View style={styles.text}>
<Text> Welcome to Home screen</Text>
</View>
</View>
);
}
}
class Profile extends React.Component{
static navigationOptions = {
drawerLabel: 'Profile',
drawerIcon: ({ tintColor }) => (
<Image
source={{uri:'http://imageholder.freeasphost.net/profile.png'}}
style={[styles.icon, { tintColor: tintColor }]}
/>
),
};
render()
{
return(
<View style={styles.container}>
<Header
leftComponent={<Icon name="menu" onPress={() => this.props.navigation.openDrawer()} />}
/>
<View style={styles.text}>
<Text>Welcome to Profile screen</Text>
</View>
</View>
);
}
}
class Settings extends React.Component{
static navigationOptions = {
drawerLabel: 'Settings',
drawerIcon: ({ tintColor }) => (
<Image
source={{uri:'http://imageholder.freeasphost.net/settings.png'}}
style={[styles.icon, { tintColor: tintColor }]}
/>
),
};
render()
{
return(
<View style={styles.container}>
<Header
leftComponent={<Icon name="menu" onPress={() => this.props.navigation.openDrawer()} />}
/>
<View style={styles.text}>
<Text>Welcome to Settings Screen</Text>
</View>
</View>
);
}
}
const myStackNavigator = createStackNavigator({
SplashScreen:{
screen:SplashScreen
},
Login:{
screen:Login
},
});
const MyDrawerNavigator = createDrawerNavigator({
Home:{
screen:Home
},
Settings:{
screen:Settings
},
Profile:{
screen:Profile
},
});
const MyApp = createAppContainer(MyDrawerNavigator);
const MyPrologue = createAppContainer(myStackNavigator);
export default class App extends Component {
render() {
return (
<MyPrologue/>
);
}
}
const styles = StyleSheet.create({
icon: {
width: 24,
height: 24,
},
container: {
flex: 1
},
text:{
flex: 1,
alignItems: 'center',
justifyContent: 'center'
},
Logocontainer:{
flex: 1,
justifyContent :"center",
alignItems:"center"
},
logo:{
width:150,
height:150
},
button:{
width:300,
padding:10,
backgroundColor:'#009999',
alignItems: 'center'
},
input: {
width: 300,
height: 44,
padding: 10,
borderWidth: 1,
borderColor: '#009999',
marginBottom: 10,
},
loginbtn:{
color:'#ffff'
},
});
For this cases i would suggest to use a SwitchNavigator, that renders pretty much like a web application. When you are passing from one screen the previous screen gets unmounted. Only the focused screen will be mounted.
I'll keep your current StackNavigator if you wanted to use it for the header:
First of all you need to import the SwitchNavigator:
import { createSwitchNavigator } from 'react-navigation';
Remove const MyApp = createAppContainer(MyDrawerNavigator); and change
const MyPrologue = createAppContainer(myStackNavigator);
to
const MyPrologue = createAppContainer(switchNavigator );
where switchNavigator is :
const switchNavigator = createSwitchNavigator({
Authentication: {
screen:myStackNavigator
},
DrawerNavigator: {
screen: MyDrawerNavigator
},
},
{
initialRouteName: "Authentication"
})
Then your login function can navigate doing :
this.props.navigation.navigate("DrawerNavigator")
You can read about SwitchNavigator at : https://reactnavigation.org/docs/en/switch-navigator.html#docsNav

Navigating between screens on a TabNavigator with the click of a button in React Native

I have set up a createMaterialTabNavigator with 4 screens. Each of those screens is a React Native component which accepts 3 properties (image, description, and nextPage). 'nextPage' is an onPress function and I am unable to get it to work.
As a workaround I've made separate components (Highlight1, Highlight2, etc.) and an onPress function in each to be able to navigate between them. But that is a lot of repeated code and that is what I want to avoid.
Below is the code for my TabNavigator where I assign each screen the corresponding React component.
routes.js
createMaterialTopTabNavigator(
{
Page1: {
screen: () => (
<Highlight
image={require('../components/Highlights/images/highlight1.png')}
description={'Description 1'}
nextPage={this.props.navigation.navigate('Page2')}
/>
)
},
Page2: {
screen: () => (
<Highlight
image={require('../components/Highlights/images/highlight2.png')}
description={'Description 2'}
nextPage={this.props.navigation.navigate('Page3')}
/>
)
},
Page3: {
screen: () => (
<Highlight
image={require('../components/Highlights/images/highlight3.png')}
description={'Description 3'}
nextPage={this.props.navigation.navigate('Page4')}
/>
)
},
Page4: {
screen: () => (
<Highlight
image={require('../components/Highlights/images/highlight4.png')}
description={'Description 4'}
/>
)
}
},
{
tabBarPosition: 'bottom',
defaultNavigationOptions: {
tabBarVisible: false
}
}
)
Highlight.js
import React, { Component } from 'react';
import { View, Text, Image, ImageBackground } from 'react-native';
import { NextButton } from '../Buttons';
import styles from './styles';
export default class Highlight extends Component {
render() {
return (
<ImageBackground style={styles.container}>
<Image
style={styles.image}
source={this.props.image}
resizeMode="cover"
/>
<View style={styles.textContainer}>
<Text style={styles.text1}>MYAPP</Text>
<Text style={styles.text2}>Highlights</Text>
<Text style={styles.text3}>{this.props.description}</Text>
</View>
<View style={styles.buttonContainer}>
<NextButton onPress={this.props.nextPage} />
</View>
</ImageBackground>
);
}
}
NextButton.js
import React from 'react';
import { View, TouchableOpacity, Text } from 'react-native';
import styles from './styles';
const NextButton = ({ onPress }) => (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<TouchableOpacity style={styles.nextButtonContainer} onPress={onPress}>
<Text style={{ color: 'white', fontSize: 14 }}>NEXT</Text>
</TouchableOpacity>
</View>
);
export default NextButton;
The above spits out the following error -> TypeError: undefined is not an object (evaluating '_this.props.navigation').
How can I get the above to work such that Page1 navigates to Page2, Page2 to Page3, and so on? Additionally, is there a better way to achieve what I am trying to do (which is basically to create a ViewPager consisting of 4 screens)?
The NextButton should be configured as follows:
<NextButton onPress={() => this.props.nextPage} />
const NextButton = (props) => {
const { onPress } = props;
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<TouchableOpacity style={styles.nextButtonContainer} onPress={onPress}>
<Text style={{ color: 'white', fontSize: 14 }}>NEXT</Text>
</TouchableOpacity>
</View>
);
};

How to add arrow-back button inside Drawer Navigator to close the Drawer when opened

I'm trying to add arrow-back button INSIDE drawer, in top-right, which close the drawer onPress. I'm not sure am I doing it right way? Or I should put a Stack Navigator as header inside the Drawer ? I'll be glad if someone help me.
I'm using react-navigation V3.
Here is my code:
import React from 'react';
import { Platform, Dimensions, View, Text, StyleSheet } from 'react-native';
import { createDrawerNavigator, createAppContainer, DrawerItems } from 'react-navigation';
import {Header, Button} from 'react-native-elements';
import Icon from 'react-native-vector-icons/FontAwesome';
import Header2 from './Header2'
class MenuButton1 extends React.Component {
render () {
const { onDrawerOpen } = this.props;
return (
<React.Fragment>
<Button
icon={
<Icon
name="bars"
size={30}
color="white"
/>
}
onPress={() => onDrawerOpen()}
/>
</React.Fragment>
)
}
}
class HomeScreen extends React.Component {
render(){
return (
<React.Fragment>
<Header
leftComponent={
<MenuButton1 onDrawerOpen = {() => this.props.navigation.openDrawer()}/>
}/>
<View style={{top: 30 }}>
<Text> Hello </Text>
</View>
</React.Fragment>
);
}
}
const CustomDrawerContentComponent = (props) => (
<View style={{top:40}}>
<Header
leftComponent={
<Button
icon={
<Icon
name="arrow-left"
size={30}
color="black"
/>
}
onPress= {() => this.props.navigation.closeDrawer()}/>}
/>
<Text>Custom Header</Text>
<DrawerItems {...props} />
</View>
),
WIDTF = Dimensions.get('window').width;
const DrawerConfig = {
drawerWidth: WIDTF*0.80,
draertType: 'slide'
}
const Drawer = createDrawerNavigator ({
Home: {
screen: HomeScreen
},
About: {
screen: Header2
}
},
DrawerConfig,
{
contentComponent: CustomDrawerContentComponent
});
export default createAppContainer (Drawer);
enter image description here
But it doesn't appear.
Here it is you can use Header with default components like:
<Header
leftComponent={{ icon: 'menu', color: '#fff' }}
centerComponent={{ text: 'MY TITLE', style: { color: '#fff' } }}
rightComponent={{ icon: 'home', color: '#fff' }}
/>
as mentioned here
Or you can do something like this:
import {NavigationActions} from 'react-navigation'; //add this import to the Navigator.js file
const DrawerNavigators = createDrawerNavigator({
Home:{
screen: Home ,
navigationOptions: ({ navigation }) => ({
headerStyle: {
backgroundColor: 'black',
headerTintColor: '#ffffff',
tintColor: {
color: '#ffffff'
},
headerTitleStyle: { color: 'black' }
},
}),
}
},{
initialRouteName: 'Home',
contentComponent: Drawers,
drawerWidth: 300
});
DrawerNavigators: {
screen: DrawerNavigators,
navigationOptions: ({ navigation }) => ({
headerTintColor: '#ffffff',
headerStyle: {
backgroundColor: 'black',
title: 'Home',
},
headerLeft:
<View style={{flex:1, flexDirection:'row'}}>
<TouchableOpacity onPress={() =>
navigation.toggleDrawer()
}>
<Image style = {{margin :15 ,height :30 ,width :30}}
source={require('./resources/menu.png')} />
</TouchableOpacity>
<TouchableOpacity onPress={()=> navigation.navigate('Home')}>
<Text style={{width: 200, fontSize:15,padding:10, color:'white',marginTop:8}}>Home</Text>
</TouchableOpacity>
</View>
,
}),
},
Now create a Drawers.js file
import React, {Component} from 'react';
import {NavigationActions,StackActions} from 'react-navigation';
import PropTypes from 'prop-types';
import {ScrollView, Text, View ,AsyncStorage,Image,TouchableOpacity} from 'react-native';
import { DrawerActions } from 'react-navigation';
import styles from './Style.js'
class Drawer extends Component {
constructor(props){
super(props)
const { navigation } = this.props;
this.state = {
my: '',
}
}
render () {
return (
<View style={{flex:1}}>
<ScrollView>
<View style={styles.headertop}>
<Image style={ styles.thumbnail } source={require('./resources/images.png')} />
<Text style={styles.headertext}>Username</Text>
<Text style={{fontSize:13, color:'white',marginTop:40, marginLeft:155}}>Wallet Balance:</Text>
<Text style={{fontSize:13, color:'white', marginTop:-20, marginLeft:260}}>$0.00</Text>
</View>
<TouchableOpacity onPress={() => this.props.navigation.navigate('MyProfile')}>
<View style={styles.menuItem}>
<Image style={styles.drawericon}
source={require('./resources/prof.png')} />
<Text style = {styles.drawerText} >
My Profile
</Text>
</View>
</TouchableOpacity>
</ScrollView>
</View>
);
}
}
Drawer.propTypes = {
navigation: PropTypes.object
};
export default Drawer;

undefined is not an object (evaluating'_this2.props.navigation.navigate') - React Native

I am having trouble navigating through screens with a simple button.
This is my App.js
export default class App extends Component<Props> {
render() {
return (
<AppStackNavigator/>
);
}
}
const AppStackNavigator = StackNavigator({
Login: { screen: LoginScreen },
Home: { screen: HomeScreen },
},{
navigationOptions: {
gesturesEnabled:false
}
})
Login.js
export default class Login extends Component {
render() {
return(
<SafeAreaView style={styles.container}>
<StatusBar barStyle='light-content'/>
<KeyboardAvoidingView behavior='padding' style={styles.container}>
<TouchableWithoutFeedback style={styles.container} onPress={Keyboard.dismiss}>
<View style={styles.logoContainer}>
<View style={styles.logoContainer}>
<Image style={styles.logo}
source={require('../images/logo/logo.png')}>
</Image>
<Text style={styles.title}>
Sports Chat App
</Text>
</View>
<View style={styles.infoContainer}>
<TextInput style={styles.input}
placeholder="Enter name"
placeholderTextColor='#ffffff'
keyboardType='default'
returnKeyType='next'
autoCorrect={false}
onSubmitEditing={()=> this.refs.phoneNumber.focus()}
/>
<TextInput style={styles.input}
placeholder="Enter phone number"
placeholderTextColor='#ffffff'
keyboardType='phone-pad'
ref={'phoneNumber'}
/>
<TouchableOpacity style={styles.buttonContainer}>
<Button title='LogIn' onPress={()=>this.props.navigation.navigate('Home')}/>
</TouchableOpacity>
</View>
</View>
</TouchableWithoutFeedback>
</KeyboardAvoidingView>
</SafeAreaView>
)
}
};
LoginScreen.js
class LoginScreen extends Component {
render(){
return (
<View>
<Login>
</Login>
</View>
);
}
}
I keep getting the error undefined is not an object (evaluating'_this2.props.navigation.navigate') whenever i try use the Login button in Login.js,
I want this to navigate to the home screen but cant figure out why this keeps happening.
If i try do it without the components it works fine.
Click to View error message
Add a navigation props to Login in LoginScreen.js, Because LoginScreen has props navigation but Login don't.
render(){
return (
<View>
<Login navigation ={this.props.navigation}>
</Login>
</View>
);
}
App.js
import React, { Component } from "react";
import { Text, View } from "react-native";
import AppStackNavigator from "./AppStackNavigator";
export default class App extends Component<Props> {
render() {
return <AppStackNavigator />;
}
}
AppStackNavigator
import React, { Component } from "react";
import { StackNavigator } from "react-navigation";
import { View, Text, TouchableOpacity } from "react-native";
const LoginScreen = props => (
<View style={{ flex: 1, justifyContent: "center", alignItems: "center" }}>
<Login navigation={props.navigation} />
</View>
);
const HomeScreen = () => (
<View style={{ flex: 1, justifyContent: "center", alignItems: "center" }}>
<Text>Home</Text>
</View>
);
const Login = props => (
<View style={{ flex: 1, justifyContent: "center", alignItems: "center" }}>
<TouchableOpacity
onPress={() => {
props.navigation.navigate("Home");
}}
>
<Text>GO to home from login</Text>
</TouchableOpacity>
</View>
);
export default (AppStackNavigator = StackNavigator(
{
Login: { screen: LoginScreen },
Home: { screen: HomeScreen }
},
{
headerMode: "none",
navigationOptions: {
gesturesEnabled: false
}
}
));
You need to access the navigation prop in the component. Check the official guide.
reactnavigation Official guide
import React from 'react';
import { Button } from 'react-native';
import { withNavigation } from 'react-navigation';
class MyBackButton extends React.Component {
render() {
return <Button title="Back" onPress={() => { this.props.navigation.goBack() }} />;
}
}
// withNavigation returns a component that wraps MyBackButton and passes in the
// navigation prop
export default withNavigation(MyBackButton);