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.
Related
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.
I am developing an android app with React Native and Native-Base.
In my component button i can't get onpress to work to change page.
import React from "react";
import { StyleSheet, View, TouchableOpacity } from "react-native";
import { NativeBaseProvider, Button, Text } from "native-base";
const ButtonAPP = (props) => {
//const linkBottom = props.linkBottom;
const textBottom = props.textBottom;
return (
<NativeBaseProvider>
<View style={styles.viewButtonAPP}>
<Button
shadow={2}
style={styles.buttonAPP}
onPress={() => props.navigation.navigate("Home")}
>
<Text style={styles.textButton}>{textBottom}</Text>
</Button>
</View>
</NativeBaseProvider>
);
};
const styles = StyleSheet.create({
styles....
});
export default ButtonAPP;
THe error is:
navigate is not defined
I don't understand why for pages in my app it works but for a component like the button it doesn't
The navigation object will be passed as props by the navigation framework only to components that are defined as screens inside a navigator. If this is not the case, then the navigation object will not be passed to this component.
You have three choices.
1) Use the useNavigation hook
const ButtonAPP = (props) => {
const navigation = useNavigation();
...
<Button
shadow={2}
style={styles.buttonAPP}
onPress={() => navigation.navigate("Home")}
>
}
2) Pass the navigation object as a prop from a parent that is a screen defined in a navigator
Assume that ScreenA is a screen that is defined in a navigator, e.g. a Stack. This would look like the following snippet.
const Stack = createStackNavigator();
function MyStack() {
return (
<Stack.Navigator>
<Stack.Screen name="ScreenA" component={ScreenA} />
</Stack.Navigator>
);
}
Assume that ButtonApp is used as a child in ScreenA. Then, we can pass the prop manually, since the framework will not do this.
const ScreenA = (props) => {
return <ButtonApp navigation={props.navigation} />
}
const ButtonAPP = (props) => {
...
<Button
shadow={2}
style={styles.buttonAPP}
onPress={() => props.navigation.navigate("Home")}
>
}
3) Let ButtonApp be a screen inside a navigator
This choice usually does only make sense for actual views in the application and not for individual components. However, this is still an option.
Using the same example as in 2). However, this will work with any navigator.
const Stack = createStackNavigator();
function MyStack() {
return (
<Stack.Navigator>
<Stack.Screen name="ButtonApp" component={ButtonAPP} />
</Stack.Navigator>
);
}
Accessing the navigation object via props as you have tried initially works in this case as well.
I am currently developing an application using React Native. I wanted to add Bottom Navigation and Tab Navigation. However, came across this bug.
As seen in the screenshot the top section is the Tab Navigation and the bottom section is the bottom navigation. Some how they got separated.
import { NavigationContainer } from '#react-navigation/native';
import { createBottomTabNavigator } from '#react-navigation/bottom-tabs';
import React from 'react';
import AroundYou from './Screens/AroundYou';
import HighlyRated from './Screens/HighlyRated';
import { createMaterialTopTabNavigator } from '#react-navigation/material-top-tabs'
const Tab = createBottomTabNavigator();
const TopNavigation = createMaterialTopTabNavigator();
function Tabs() {
return (
<Tab.Navigator screenOptions={{
headerShown: false, tabBarAllowFontScaling: false, tabBarStyle: {
backgroundColor: "#181A20"
}
}} >
<Tab.Screen name="Home" component={AroundYou}/>
</Tab.Navigator>
)
}
const App = () => {
return (
<>
<NavigationContainer>
<TopNavigation.Navigator>
<TopNavigation.Screen name="AroundYou" component={AroundYou} />
<TopNavigation.Screen name="HighlyRated" component={HighlyRated} />
</TopNavigation.Navigator>
</NavigationContainer>
<NavigationContainer>
<Tabs />
</NavigationContainer>
</>
)
}
export default App;
this is the code for the Top Tab Navigation as well as the bottom navigation. I am kind of know what it causing this bug but not sure on how to get around it.
Thank you
I'm trying to change screens and I can't, it doesn't report any errors, the button just doesn't work, I click and it doesn't change screens.
SignIn.tsx
import { useNavigation } from "#react-navigation/native";
export function SignIn(){
const navigation = useNavigation();
function handleSignIn() {
navigation.navigate('Home')
}
return(
<View style={styles.container}>
<ButtonIcon
title="Next"
activeOpacity={0.7}
onPress={handleSignIn}
/>
</View>
)
}
Routes.tsx
import React from 'react';
import { createStackNavigator } from '#react-navigation/stack';
import { Home } from '../screens/Home';
import { SignIn } from '../screens/SignIn';
const { Navigator, Screen } = createStackNavigator();
export function AuthRoutes() {
return(
<Navigator screenOptions={{headerShown: false}}>
<Screen name="SignIn" component={SignIn}/>
<Screen name="Home" component={Home}/>
</Navigator>
)
}
As I said, there is no error in the code, at least according to the IDE there is no error, I can run the application, but when I click the button it has no effect, nothing happens. I've already modified a lot, added and removed, but I couldn't make it work, I don't know what I'm doing wrong.
I want to create a splashscreen without the whitescreen issue using the react-native splash screen and after that I want to check the navigation. Suppose I got 3 screen Splash, Login and Home. If the user is logged in I want to navigate to the Home screen from the Splash screen otherwise I want to navigate it to the Login page from the Splash screen. How do I achieve this using react-native-splash-screen and react-navigation.
App.js
import React from 'react';
import {ActivityIndicator} from 'react-native';
import {Provider} from 'react-redux';
import {SafeAreaProvider} from 'react-native-safe-area-context';
import {PersistGate} from 'redux-persist/es/integration/react';
import Navigator from './app/navigation/NavigationStack';
import configureStore from './app/store/configureStore';
const {persistor, store} = configureStore();
export default function App() {
return (
<SafeAreaProvider>
<Provider store={store}>
<PersistGate loading={<ActivityIndicator />} persistor={persistor}>
<Navigator></Navigator>
</PersistGate>
</Provider>
</SafeAreaProvider>
);
}
NavigationStack.js
function MyStack() {
return (
<NavigationContainer ref={navigationRef}>
<Stack.Navigator
screenOptions={{
headerShown: false,
}}>
<Stack.Screen
name={Strings.string_SignInScreen}
component={SignInScreen}
/>
<Stack.Screen name={Strings.string_HomeScreen} component={Home} />
</Stack.Navigator>
</NavigationContainer>
);
}
export default MyStack;
You can do something like this,in its componentDidMount cehck fro user exits via asyncstorage etc, and then navigate accordingly
import SplashScreen from 'react-native-splash-screen'
export default class WelcomePage extends Component {
componentDidMount() {
// do stuff while splash screen is shown
// After having done stuff (such as async tasks) hide the splash screen
if(userExists){
SplashScreen.hide();
this.props.navigation.navigate('Homescreen');
} else {
this.props.navigation.navigate('LoginScreen');
}
}
}
Hope it helps. feel free for doubts