Home > Web Front-end > JS Tutorial > body text

How to handle navigation from one page to another in React Native?

PHPz
Release: 2023-09-05 23:37:07
forward
761 people have browsed it

如何在 React Native 中处理从一个页面到另一页面的导航?

While developing the application we want to switch from one screen to another and this is handled through react navigation.

To work on the navigation page, we need to install some packages as shown below - p>

npm install @react-navigation/native @react-navigation/stack
npm install @react-native-community/masked-view react-native-screens react-native-safe-area-context react-native-gesture-handler
Copy after login

After completing the above installation, now let us proceed to the next navigation setup in React Native.

Create a folder called Pages/ in your application project. Create 2 js files HomePage.js and AboutPage.js.

pages/HomePage.js

import * as React from 'react';
import { Button, View, Alert, Text } from 'react-native';
const HomeScreen = ({ navigation }) => {
   return (
      <Button title="Click Here" onPress={() => navigation.navigate(&#39;About&#39;, { name: &#39;About Page&#39; })}/>
   );
};
export default HomeScreen;
Copy after login

In the home page, we want to display a button titled "Click Here". Clicking this button will navigate the user to the AboutPage screen. The details of

AboutPage are as follows -

pages/AboutPage.js

import * as React from &#39;react&#39;;
import { Button, View, Alert, Text } from &#39;react-native&#39;;
const AboutPage = () => {
   return <Text>You have reached inside About Page!</Text>;
};
export default AboutPage;
Copy after login

In About page we just display the text as shown above.

Now let us call the page in App.js as follows -

The page is called as follows -

import HomePage from &#39;./pages/HomePage&#39;;
import AboutPage from &#39;./pages/AboutPage&#39;;
Copy after login

Additionally, we need to call the page from @react-navigation/native Importing NavigationContainer will act as a navigation container. Add createStackNavigator from @react-navigation/stack.

Call createStackNavigator() as shown below -

const Stack = createStackNavigator();
Copy after login

Now you can add pages to this stack using as the parent container. Stack.Navigation helps your app transition between screens, with each new screen placed on top of the stack.

<NavigationContainer><Stack.Navigator><Stack.Screen name="Home" component={HomePage} options={{ title: &#39;From home page : Navigation&#39; }} /><Stack.Screen name="About" component={AboutPage} />
</Stack.Navigator></NavigationContainer>
Copy after login

To create a stack for the Home screen, please complete it as follows-

<Stack.Screen name="Home" component={HomePage} options={{ title: &#39;From home page : Navigation&#39; }} />
Copy after login

To create a stack for the AboutPage screen, please complete it as follows-

<Stack.Screen name="About" component={AboutPage} />
Copy after login

Here is the Complete code to provide help in navigation screen in React Native -

import * as React from 'react';
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
import HomePage from &#39;./pages/HomePage&#39;;
import AboutPage from &#39;./pages/AboutPage&#39;;
const Stack = createStackNavigator();
const MyStack = () => {
   return (
      <Stack.Screen name="Home" component={HomePage} options={{ title: &#39;From home page : Navigation&#39; }} /><Stack.Screen name="About" component={AboutPage} />
      
   );
};
export default MyStack;
Copy after login

The above is the detailed content of How to handle navigation from one page to another in React Native?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!