How to add a NavigationDrawer in react native? - react-native

I know this question has been asked before, but for my situation I can't find an answer. I am helping make an app with a friend and I am tasked with making a DrawerNavigator for one of our screens. I am using react native for this.
Here is my render function inside the class of one of my screens
render() {
return (
<Container>
<Header title='BarMate'/>
<View style={styles.container}>
<MapView
style={{ alignSelf: 'stretch', height: 750}}
region={this.state.mapRegion}
onRegionChange={this._handleMapRegionChange}
/>
</View>
</Container>
);
}
The problem is that when I try to add an icon and button to the Header, it just doesn't show up. Here is the code after I added the icon
render() {
return (
<Container>
<Header title='BarMate'>
<Left>
<Icon name="ios-menu" onPress={() =>
this.props.navigation.navigate('DrawerOpen')} />
</Left>
</Header>
<View style={styles.container}>
<MapView
style={{ alignSelf: 'stretch', height: 750}}
region={this.state.mapRegion}
onRegionChange={this._handleMapRegionChange}
/>
</View>
</Container>
);
}
However, this code does work if instead of importing Container and Header from a custom made file like this:
import { Container } from '../components/Container'
import { Header } from '../components/Header'
I import them like this:
import {Container, Header, Icon, Button, Content, Left } from 'native-base'
I'm a beginner in react native so there might just be something here I'm overlooking, but any help would be appreciated. Here is what's in the files Container and Header my friend made.
//Header file
import React from 'react';
import { View, Text, StatusBar } from 'react-native';
import { Icon, Button, Content, Left} from 'native-base'
import styles from './styles';
const Header = ({title}) => (
<View style={styles.container}>
<StatusBar translucent={false} barStyle="light-content" />
<Text style={styles.title}>{title}</Text>
</View>
);
export default Header;
//Container file
import React from 'react';
import PropTypes from 'prop-types'; // ES6
import { View } from 'react-native';
import styles from './styles';
const Container = ({ children }) => (
<View style={styles.container}>
{children}
</View>
);
Container.propTypes = {
children: PropTypes.any,
}
export default Container;

Related

Put Avatar and icon on Top of page without React native Element Headers

I am trying to do something like this.
i am using React native Elements, and when i add the components it brings something like a very blue header, hence disrupts other screens and i would not want the blue header on all the screens.
Now i am looking for a way to have the avatar on the top left and the "+" sign on the top right without the blue header stuff showing.
This is what it looks like when i removed the header from React native Elements
And this is what the source code looks like so far
import React from 'react';
import {StyleSheet, Text, View} from 'react-native';
import {Header, Avatar, Icon} from '#rneui/themed';
const HomePage = () => {
return (
<View>
<Avatar
small
rounded
source={{
uri: 'http://www.usanetwork.com/sites/usanetwork/files/styles/629x720/public/2016/07/mrrobot_s2_cast_rami-malek2.jpg',
}}
onPress={() => console.log('Left Clicked!')}
activeOpacity={0.7}
/>
<Icon
name={'add-circle-outline'}
color={'#00BB23'}
size={32}
onPress={() => console.log('Right Clicked!')}
/>
</View>
);
};
export default HomePage;
const styles = StyleSheet.create({});
Is there something i am not doing rightly? Pls advice, i need to know.
Once again , I got it to work.
I used the header and set the parameters to 'Transparent' and it works fine, then I used npm to install this package react-native-safe-area-view
like so npm i react-native-safe-area-view
Then using it inside my codes , it worked fine.
Codes for example
import { StyleSheet, Text, View } from 'react-native'
import React from 'react'
import {useSafeArea} from 'react-native-safe-area-view';
const ProfilePage = () => {
const safeArea = useSafeArea();
return (
<View style={{flex: 1, paddingTop: safeArea.top}}>
<Text>ProfilePage</Text>
</View>
)
}
export default ProfilePage
const styles = StyleSheet.create({})
Then for the transparent header for React native Elements
I used this
import React from 'react';
import {StyleSheet, Text, View} from 'react-native';
import {Header, Avatar, Icon} from '#rneui/themed';
const HomePage = () => {
return (
<View>
<Header
containerStyle={{
backgroundColor: 'transparent',
justifyContent: 'space-around',
}}
leftComponent={
<Avatar
small
rounded
source={{
uri: 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSiRne6FGeaSVKarmINpum5kCuJ-pwRiA9ZT6D4_TTnUVACpNbzwJKBMNdiicFDChdFuYA&usqp=CAU',
}}
onPress={() => console.log('Left Clicked!')}
activeOpacity={0.7}
/>
}
rightComponent={
<Icon
name={'add-circle-outline'}
color={'#00BB23'}
size={32}
onPress={() => console.log('Right Clicked!')}
/>
}></Header>
</View>
);
};
export default HomePage;
const styles = StyleSheet.create({});
where I had to import rneui/themed using Npm packages
And everything works fine, as expected
Thanks everyone!

how to go to another page in react native?

how to move from one page to another in react native, please help me I am new to react-native I want to move from this page to another by clicking a button but I don't know how to do this
this is splash screen JSON file
import React from 'react';
import { View, Text, Button, Dimensions, StyleSheet, Image, TouchableOpacity} from 'react-native';
//import { LinearGradient } from 'react-native-svg';
//import {LinearGradient} from 'react-native-linear-gradient';
//import { LinearGradient } from "react-native-svg";
//import {MaterialIcons} from 'react-native-vector-icons';
import * as Animatable from 'react-native-animatable';
//import { NavigationContainer } from '#react-navigation/native';
//import SignInScreen from './SignInScreen';
const SplashScreen = ({navigation}) => {
return (
<View style={styles.container}>
<View style={styles.header}>
<Animatable.Image animation="bounceIn" duraton="1500" source=
{require('../components/images/roz.jpg')} style={styles.logo} resizeMode='stretch'/>
</View>
<Animatable.View animation="fadeInUpBig" style={styles.footer}>
<Text style={styles.title}>Stay connected with everyone!</Text>
<Text style={styles.text}>Sign in with account</Text>
<View style={styles.button}>
<TouchableOpacity onPress={() => navigation.navigate('SignInScreen')} >
<Text style={styles.textSign}>Get Started</Text>
</TouchableOpacity>
</View>
</Animatable.View>
</View>
);
};
export default SplashScreen;
const {height} = Dimensions.get("screen");
const height_logo = height * 0.28;
you have to implement React Navigation library as described on official page
Navigating to a new screen
import * as React from 'react';
import { Button, View, Text } from 'react-native';
import { NavigationContainer } from '#react-navigation/native';
import { createStackNavigator } from '#react-navigation/stack';
function HomeScreen({ navigation }) {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Home Screen</Text>
<Button
title="Go to Details"
onPress={() => navigation.navigate('Details')}
/>
</View>
);
}

Drawer can be seen when swiping back

I'm developing mobile application with the below technologies.
- react-native
- native-base (Especially, Drawer component)
- react-native-router-flux
And I have a problem with mine.
My application has Drawer on the left side, and it can be shown from the left side on the screen when clicking the icon on the header.
However, when swiping back, the Drawer component can be seen but not as Drawer, it is like a screen that attaches on the left side.
This link is the GIF animation that reproduces my problem.
https://gyazo.com/a9109844199bae04f898431edbeaddbc
These are my code.
DrawerComponent
import React, { Component } from 'react';
import { Drawer, Hdeader, Left, Icon } from 'native-base';
import SidebarContainer from '../component/SidebarConmponent';
:
export default class DrawerComponent extends Component {
:
render() {
return (
<Drawer
ref={(ref) => { this.drawer = ref; }}
content={<SidebarComponent navigator={this._navigator} userId={this.props.userId} firebase={this.props.firebase}/>}
openDrawerOffset={0.4}
tapToClose={true}
onClose={() => this.closeDrawer}
onOpen={() => this.openDrawer}
styles={{ height: 100 }}
side={'left'}
type={'overlay'}
>
<Header style={{ backgroundColor: 'transparent' }}>
<Left style={{ flexDirection: 'row'}}>
<Icon onPress={this.openDrawer.bind(this)} type='MaterialIcons' name='menu' style={{ color: 'white', justifyContent: 'center' }} />
</Left>
{this.props.children}
</Drawer>
:
SiderbarComponent
:
import React, { Component } from 'react';
import { Container, Content, Header, Left, Icon, List, ListItem } from 'native-base';
export default class SidebarContainer extends Component {
:
render() {
return (
<Container>
<View style={{ height: height }}>
<Content>
<List>
<ListItem button>
<Icon type='Ionicons' name='md-home' />
<Text style={{ paddingLeft: 10 }}Home</Text>
</ListItem>
</List>
</Content>
</View>
</Container>
)
}
}
HomeComponent
import React, { Component } from 'react';
import { Text } from 'react-native';
import DrawerComponent from '../component/DrawerComponent';
export default class HomeComponent extends Component {
render(
return (
<DrawerComponent>
<Text>Home</Text>
</DrawerComponent>
)
)
}
Home component extends from wrong.need to extends from Component import
try this like this
import React from 'react'
import { Text } from 'react-native';
import DrawerComponent from '../component/DrawerComponent';
export default class Home extends React.Component {
render(){
return (
<DrawerComponent>
<Text>Home</Text>
</DrawerComponent>
)
}
}

Styling custom component in react native

I am trying to add styling to my custom component in react native, but no matter what I do, the style has no effect. Here is my code:
// App.js
import MyCustomComponent from './components/myCustomComponent.js';
render() {
return (
<View style={styles.container}>
<MyCustomComponent style={{marginTop: 10}}/>
</View>
);
}
The project compiles fine, and my custom component appears on screen fine, but the marginTop styling is not applied. It is worth noting that the style for the parent View component does apply correctly. This is a brand new project I just created today. This seems like it should be extremely basic, but just isn't working. What can I do to apply this styling?
Custom component code:
import React, {Component} from 'react';
import {TextInput, StyleSheet, Image, View, Button} from 'react-native';
type Props = {};
export default class MyCustomComponent extends Component<Props> {
render() {
return (
<View style={styles.container}>
<Image
source={{ uri: "source here" }}
style={{ width: 50, height: 50 }}
/>
<TextInput
style={{ height: 50 }}
placeholder="Search"
/>
</View>
)
}
}
you can use this code:
export default class MyCustomComponent extends Component<Props> {
render() {
return (
<View style={[styles.container, {...this.props.style}]}>
...
</View>
)
}
}
now, styles.container is applied and anything you pass to component through style will be added to component style.
I hope this can help you
You can apply a style to your custom component by passing style as props.
and
Use it as style={this.props.style} in your MyCustomComponent.
import React, {Component} from 'react';
import {TextInput, StyleSheet, Image, View, Button} from 'react-native';
type Props = {};
export default class MyCustomComponent extends Component<Props> {
render() {
return (
<View style={[styles.container,{...this.props.style}]}>//<--Use like this---
<Image
source={{ uri: "source here" }}
style={{ width: 50, height: 50 }}
/>
<TextInput
style={{ height: 50 }}
placeholder="Search"
/>
</View>
)
}
}
add this code in your CustomText.js file (custom component):
import React from 'react'
import {Text, StyleSheet} from 'react-native'
const CustomText = props => {
return (<Text {...props} style={{...styles.text, ...props.style}}>{props.children}</Text>);
}
export default CustomText;
const styles = StyleSheet.create({
text:{
color: '#000'
}
})
and use in the file:
<CustomText style={styles.text}>My text</CustomText>
const styles = StyleSheet.create({
text:{
fontSize: 20,
}
});
this code merge styles and pass all property to the custom components.
For example, lets change background color of custom card.
Custom Card:
export default function MyCard({color}) {
return (
<View style={[styles.card, {backgroundColor: color}]}>
</View>
)
}
In another file
<MyCard color={"pink"} />
Here, styles.card is the style added in Custom Card file and the color is given during component use.
Note: MyCard({color}) if you miss to add highlight parentheses, it will not work. I faced this issue.
You need to apply this style yourself inside MyCystomComponent. For example:
const MyCustomComponent = ({style}) => (
<View style={style}> // This will be the style that is passed as a prop.
</View>
);

React navigation this.props.navigation undefined

I have a custom header like this
import React from 'react';
import { View, TouchableOpacity, Text, StyleSheet, Image } from 'react-native';
import { Header, Left, Right, } from 'native-base';
import { Icon } from 'react-native-elements';
export default class MainHeader extends React.Component {
pressSearch() {
this.props.navigation.navigate('Login');
}
render () {
return(
<Header style={{paddingTop:25}}>
<Left style={{marginBottom:10}}>
<TouchableOpacity>
<View style={{flexDirection:'row'}}>
<Image
style={styles.stretch}
source={require('../images/logoforplay.png')}
/>
<Text style={styles.headerText} > Eventlinn </Text>
</View>
</TouchableOpacity>
</Left>
<Right>
<TouchableOpacity
style={styles.headerButton}
onPress={this.pressSearch.bind(this)}
>
<Icon
name={'search'}
/>
</TouchableOpacity>
<TouchableOpacity
style={styles.headerButton}
>
<Icon
name={'add-location'}
/>
</TouchableOpacity>
<TouchableOpacity
style={styles.headerButton}
>
<Icon
name={'notifications'}
/>
</TouchableOpacity>
</Right>
</Header>
)
}
}
const styles = StyleSheet.create ({
mainContainer: {
flex:1,
backgroundColor:'white'
},
cardContainer: {
flex:1
},
stretch: {
height:35,
width:35,
},
headerText: {
fontSize:24,
marginTop:3,
},
headerButton: {
marginBottom:10,
marginLeft:15
}
})
My problem navigation.navigate not working.My routers working well because i can navigate to 'login' with another screen.I read something about pure components but still dont understand how to solve this problem.What do you suggest ? By the way i am new to react native.
If your header isn't a route within your Router at the head of your app you will need to use withRouter from react-router
import {withNavigation} from 'react-navigation'
export default withNavigation(MainHeader)
Now you should be able to access this.props.navigation without directly passing it down as a prop from a parent component.
use import { withNavigation } from 'react-navigation'; at the top and then export default withNavigation(FilmItem); at the end.
See doc : https://reactnavigation.org/docs/en/connecting-navigation-prop.html