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

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'
}
});

Related

Image not showing in React Native Web App. Appears when I build for Android or iOS but doesn't show when using react-scripts start

I am trying to display an image in a React Native Web App which is run using react-scripts start. When I build the App for iOS or Android, the image appears perfectly fine (using expo) but when I build it for the Web App, the image fails to load. Here is the code snippet for the Home component where the image is loaded
import React from "react";
import { ScrollView, ActivityIndicator, StyleSheet, Image, ImageBackground } from "react-native";
import UserList from "./user-list";
import Header from './header';
import sanityClient from './assets/client'
import BackButton from './back-button'
import User from './user'
// import {Asset} from 'expo-asset';
// const imageURI = Asset.fromModule(require('./arrow.png')).uri;
const image = require('./assets/aoeu.jpg');
class Home extends React.Component {
state = {
user: {},
loading: true
};
componentDidMount() {
// TODO: get users
this.getUser();
}
async getUser() {
sanityClient.fetch(`*[ _type == "user" && emailAddress.current == "dwight#viamaven.com"]`)
.then((data) => {
console.log(data);
this.setState({user: data[0], loading: false});
console.log(this.state.user);
})
.catch((err) => console.error(err))
// const res = await fetch("https://randomuser.me/api/?results=20");
// const { results} = await res.json();
// // console.log(results)
// this.setState({users: [...results], loading: false});
}
render() {
return (
<ScrollView
noSpacer={true}
noScroll={true}
style={styles.container}
showVerticalSCrollIndicator = {false}
showHorizontalScrollIndicator = {false}
>
{this.state.loading ? (
<ActivityIndicator
style={[styles.centering, styles.gray]}
color="#5d38aa"
size="large"
/>
) : (
<View>
<Header title={this.state.user.name} />
<View>
<Image
source={require('./arrow.png')}
style={styles.image}
/>
</View>
<User />
</View>
)}
</ScrollView>
);
}
}
var styles = StyleSheet.create({
container: {
backgroundColor: "white",
width: '375px',
height: '812px',
// top: '50px',
},
centering: {
alignItems: "center",
justifyContent: "center",
padding: 8,
height: '100vh'
},
image: {
width: '50px',
height: '50px',
marginRight: 20,
boxShadow: "0 1px 2px 0 rgba(0,0,0,0.1)"
}
});
export default Home;
Here is a link to the GitHub repo where the entire project is stored https://github.com/nimbusdin/stackreactnative
Try to import this way and use it like this
import image = './assets/aoeu.jpg';
<Image
source={image}
style={styles.image}
/>

React-Native Show an image illustration when there is no network (No internet connection status)

I am (newbie) working on an iOS app using react-native. I want to show an image illustration when app goes offline or there is no internet connection to inform the user. I have done all set up required such as :-
1. My image is at src/assets folder
2. using this image in screen - which will be displayed when app detects no internet connection.
In iOS simulator it works fine, but when I run it on iPhone it does not show that image only text appears.
NoInternetScreen.js
import React from 'react';
import {View, StyleSheet, Image, Text} from 'react-native';
const NoInternetScreen = props => {
return(
<View style={styles.imageContainer}>
<Image style={styles.image} source={require('../assets/NoConnection.png')}/>
<View style={styles.textContainer}>
<Text style={styles.text}>No Internet connection</Text>
</View>
</View>
)
}
const styles = StyleSheet.create({
imageContainer: {
flex:1,
justifyContent: 'center',
alignItems: 'center',
marginHorizontal: 20,
},
image: {
width: 160,
height: 220,
alignSelf: 'center',
},
textContainer: {
marginVertical: 15,
},
text: {
fontSize: 20,
}
});
export default NoInternetScreen;
Login.js - which uses NoInternetScreen
import React, {useContext} from 'react';
import {
View,
Text,
StyleSheet
} from 'react-native';
import {NetworkContext} from '../NetworkContext';
import NoInternetScreen from './NoInternetScreen';
const Login = props => {
const isConnected = useContext(NetworkContext);
return (
isConnected ? <View style={styles.container}>
<Text> Login </Text>
</View> : <NoInternetScreen/>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
}
});
export default Login;
Could you please suggest what could be done to fix it?
Thanks and Regards,
Ankur

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.

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.

Nesting Stack Navigator with Drawer Navigator

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
}
}
}
);

Resources