Trying to navigate to a webview on my mobile app - react-native

That's my code. I am working with expo. This just shows a blank white background but the URL doesn't load up
On expo documentation page. it seems pretty easy but I have no idea if it is cause I am navigating to the webview that's why I am facing this issue. P.S no error or warning is shown

1.) create Expo Project
expo init rnwebviewnav
2.) install expo react navigation from react navigation
npx expo install react-native-screens react-native-safe-area-context
3.) install webview with expo
expo install react-native-webview
4.) Create a folder called src (for source) and add your files. For me, I added 2 files there Like this
5.) make sure your package.json looks like this (or has this components as dependencies)
6.) Make sure your App.js Looks like this
import React from "react";
import { NavigationContainer } from "#react-navigation/native";
import { createStackNavigator } from "#react-navigation/stack";
import WebViewPage from "./src/WebViewPage";
import ButtonPage from "./src/ButtonPage";
const Stack = createStackNavigator();
const App = () => {
return (
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen
name="ButtonPage"
component={ButtonPage}
options={{ headerShown: false }}
/>
<Stack.Screen
name="WebViewPage"
component={WebViewPage}
options={{ title: "Webview Page" }}
/>
</Stack.Navigator>
</NavigationContainer>
);
};
export default App;
7.) Make sure the page that navigates to the Webview Looks like this
Button Page
import { Button, StyleSheet, Text, View } from 'react-native'
import React from 'react'
import { useNavigation } from '#react-navigation/native';
const ButtonPage = () => {
const navigation = useNavigation();
const navigateToWebView = () =>{
navigation.navigate('WebViewPage');
}
return (
<View style={{flex:1, justifyContent:'center'}}>
<Text>ButtonPage</Text>
<Button onPress={navigateToWebView} title="Click to Navigate" color="#841584" />
</View>
)
}
export default ButtonPage
const styles = StyleSheet.create({})
8.) Make sure your Webview Page looks thus
Web View Page
import { SafeAreaView, StyleSheet, Text, View } from 'react-native'
import React from 'react'
import WebView from 'react-native-webview'
const WebViewPage = () => {
return (
<SafeAreaView style={{ flex: 1 }}>
<WebView
source={{ uri: 'https://reactnative.dev/' }}
/>
</SafeAreaView>
)
}
export default WebViewPage
const styles = StyleSheet.create({})
9.) run npm start on your terminal to see what you have done so far!
button page
webview page
I believe I have answered your question :) If yes , pls upvote my answer if it has helped you.

Related

Screen not showing up when using Stack.Navigator in expo

I am new to app development and learning react-native using youtube tutorials.
I am trying to use Stack.Navigator for navigation and when I run the app using Expo Go on my mobile, nothing shows up. I don't see any compilation errors. Expo Go reloads the screen. But, nothing shows up on the screen. It is blank.
Here's the code:
App.js
import SignInScreen from '../screens/SignInScreen';
const Stack = createNativeStackNavigator();
const Navigation = () => {
return (
<NavigationContainer>
<Stack.Navigator screenOptions={{headerShown: false}}>
<Stack.Screen name="SignIn" component={SignInScreen} />
</Stack.Navigator>
</NavigationContainer>
);
};
export default Navigation;
In App.js, if I change it to a text and reload, I see my app shows up as Navigation on the screen. Would appreciate any help.
const Navigation = () => {
return (
<NavigationContainer>
<Text>Navigation</Text>
</NavigationContainer>
);
};
import { StyleSheet, Text, View } from 'react-native';
import Navigation from './Navigation';
export default function App() {
return (
<View style={styles.container}>
< Navigation />
</View>
);
}
const styles = StyleSheet.create({
container:{
flex:1,
}
});
YOUR CODE IN APP.JS SHOULD LOOK LIKE THIS IF YOU ARE IMPORTING NAVIGATION FROM SOME FILE.

I am using Navigate to go to another page on React Native, I've tried with navigation & React Dom

I am using Navigate to go to another page on React Native, I've tried with navigation & react Dom. I want to go to Home.js. I've tried using NavigationContainer, & reateStackNavigator and also React Dom.
I can't seem to find a solution to either of the two and the YouTube videos aren't really helping.
import {
StyleSheet,
Text,
SafeAreaView,
Image,
View,
TouchableOpacity,
} from 'react-native';
import React,{ useState} from 'react';
import {Card} from 'react-native-shadow-cards';
import { NavigationContainer } from '#react-navigation/native';
import { createStackNavigator } from '#react-navigation/stack';
import {Route, BrowserRouter as Router, Switch, Link, Navigate, NavigationType} from 'react-router-dom';
export default function App() {
const handlePress = () => navigation.navigate('Home');
const [name,setName] = useState("");
const shadowstyle={
shadowOpacity:1
};
return (
<SafeAreaView style={styles.container}>
<Image
source={require("/Users/travisnunnally/Malik Projects VSCODE/Gtipz/assets/Star.png")} />
<Image
source={require("/Users/travisnunnally/Malik Projects VSCODE/Gtipz/assets/Star.png")} />
<Text style={styles.titleText} onPress={handlePress}>GTipz </Text>
<Image
source={require("/Users/travisnunnally/Malik Projects VSCODE/Gtipz/assets/GtipzLogo.png")} />
<View>
<Image
source={require("/Users/travisnunnally/Malik Projects VSCODE/Gtipz/assets/Star.png")} />
</View>
<TouchableOpacity onPress={handlePress}>
<View style ={[styles.buttonContainer, styles.userImageWrapper]}>
<Text style={styles.buttonText}>Login</Text>
</View>
You need to import the useNavigation hook from react-navigation.
import { useNavigation } from '#react-navigation/native'
const navigation = useNavigation()
I also suggest you replace Text with TouchableOpacity where you call the handlePress function
Please refer to this doc for more information.

Error: Couldn't find a navigation object. Is your component inside NavigationContainer?

I am trying to navigate from my login screen to the home screen using a pressable button. However, the code that I now have gives the following error: Couldn't find a navigation object. Is your component inside NavigationContainer?. The error is aimed on row 8 (const navigation = useNavigation();).
LoginButton.js:
import React, { Component } from "react";
import { View, Text, Image, Pressable, Button } from "react-native";
import { useNavigation } from "#react-navigation/native";
import styles from "./styles";
import HomeScreen from "../../screens/Home";
const LoginButton = () => {
const navigation = useNavigation();
return (
<View style={styles.container}>
<Pressable
onPress={() => navigation.navigate("Home")}
style={styles.button}
>
<Text style={styles.buttontext}>Login</Text>
</Pressable>
</View>
);
};
export default LoginButton;
The LoginButton is inserted as a component in the LoginItem inside the last <View. LoginItem.js:
import React from "react";
import { View, Text, Image } from "react-native";
import styles from "./styles";
import LoginButton from "../LoginButton";
import { createNativeStackNavigator } from "#react-navigation/native-stack";
const Stack = createNativeStackNavigator();
const LoginItem = (props) => {
return (
<View style={styles.logincontainer}>
{/* Background image of the login screen */}
<Image
source={require("../../../assets/images/blue-sky-start-screen.jpg")}
style={styles.loginbackground}
blurRadius={4}
></Image>
{/* Title of the login screen */}
<View style={styles.titlecontainer}>
<Text style={styles.companytitle}>BestelSnel</Text>
</View>
{/* Login button */}
<View style={styles.loginbutton}>
<LoginButton></LoginButton>
</View>
</View>
);
};
export default LoginItem;
useNavigation only works if your component is inside of a NavigationContainer and a Navigator like mentioned on the getting-started-page:
https://reactnavigation.org/docs/hello-react-navigation
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen name="Home" component={HomeScreen} />
</Stack.Navigator>
</NavigationContainer>
HomeScreen would be your LoginItem
In my case, I got this error because autocomplete added the wrong import statement.
import { useNavigation } from "#react-navigation/core";
but the one I needed is:
import { useNavigation } from "#react-navigation/native";
Hope this helps anybody to save some minutes.
I had this issue as well, and the accepted answer did not work for me.
What I did get working on was importing the component I wanted to go to and using that in the navigate() call. Generally, this probably means just removing the quotes so your param to that function is a Component and not a String.
Given the code from the question, the code would be:
onPress={() => navigation.navigate(Home)}
Very similar to the original, just minus the quotes, as mentioned.
I was passing the params correctly also, but params had a react component in it. That's why it was giving this error.
// product should not have a react component
props.navigation.navigate(ROUTE_TEST, {product});

Having problems when adding module Navigation with ReactNative 0.65.1

I'm trying to add module BottomNavigation and DrawerNavigation. I did download the module package successfully. I conducted import createBottomTabNavigator() or createDrawerNavigator() then i received error notify as shown below.
Can anyone help me?
Update:
I installed them by npm install #react-navigation/bottom-tabs.
And my code:
import React from 'react';
import Home from '../Component/Home';
import { NavigationContainer } from '#react-navigation/native';
import { createBottomTabNavigator } from '#react-navigation/bottom-tabs';
const Tab = createBottomTabNavigator();
export default function BottomNav(){
return(
<NavigationContainer>
<Tab.Navigator
initialRouteName="Home"
>
<Tab.Screen name="Home" component={Home} />
</Tab.Navigator>
</NavigationContainer>
)
}
react native 0.65.1 had some issues with reanimated library. thats why happens, also i got same issue.

React native navigation (web, android and ios) Goes back to first screen

Hey I'm using react native to build an app for android, ios and web, I'm using https://reactnative.dev/docs/navigation for navigation, it works fine on android and IOs but on web I've stumbled open 2 problems.
First is the next screen always open in the same page, I want it to go to a new page.
Second, Let's assume I'm on the third screen in the stack and if I hit refresh in the browser It goes to the first screen(in my case the first screen is the login screen).
What can I do to solve this problem? is there any other navigation framework that I should use for web, android and ios, or do you have completely different thing to suggest?
import 'react-native-gesture-handler';
import { StatusBar } from 'expo-status-bar';
import React from 'react';
import {
StyleSheet,
Text,
View,
} from 'react-native';
import { NavigationContainer, StackActions } from "#react-navigation/native";
import { createStackNavigator } from "#react-navigation/stack";
import LoginScreen from './screen/LoginScreen';
import HomeScreen from './screen/HomeScreen';
import AboutUsScreen from './screen/AboutUsScreen';
import ProgramsScreen from './screen/ProgramsScreen';
import ResourcesScreen from './screen/ResourcesScreen';
import NewsScreen from './screen/NewsScreen';
const Stack = createStackNavigator();
export default function App() {
return (
<NavigationContainer>
<Stack.Navigator
screenOptions={{ headerShown: true }}
>
<Stack.Screen name="LoginScreen" component={LoginScreen} />
<Stack.Screen name="HomeScreen" component={HomeScreen}/>
<Stack.Screen name="AboutUsScreen" component={AboutUsScreen}/>
<Stack.Screen name="ProgramsScreen" component={ProgramsScreen}/>
<Stack.Screen name="ResourcesScreen" component={ResourcesScreen}/>
<Stack.Screen name="NewsScreen" component={NewsScreen}/>
</Stack.Navigator>
</NavigationContainer>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
For anyone looking for an answer, I solved it, I created 2 App.js files, App.js for web and App.android.js for android, In App.android.js I used react navigation for navigating and for the web I used routes, Using routes removed the problem which was on refreshing the page the app would jump to the login screen.