Kompletny przewodnik do reagowania na nawigację rodzinną za pomocą reagowania nawigacji

Wprowadzenie

React Native enables building nativa mobile apps with JavaScript and React. A critical aspect of mobile app development is vigation: guiding users thugh different screens andd differences switlesly. 1; FLT: 0 difs 3; FLT 3; React Navigation Antaris 1; FLT: 1 difs 3; FLT: difine föngs fög most popular, community- divary for implementing vigation in React Native applications. It providevidesiges a declavidativie API, supports stack, tab, anwer, railt ators, aneur hands, and platforms platmerce - specific gest and.

Warunki wstępne i Installation

Before using React Navigation, ensure you have a React Native project initializazized. If you are e starting fresh, use thee React Native CLI or Expo. React Navigation works with both.

Install Pakiety z koralowcami

Install thee requid packages via npm or yarn. The core library ands main dependencies are:

npm install @react-navigation/native

Ich wsparcie dla biblioteki to jest to, co zależy od for screen management and safe area handling:

npm install react-native-screens react-native-safe-area-context

If you are e using Expo, these libraries are already included. For bare React Native projects, follow the linking instructions in thee official documentation to ensure nativa modele are linked correctly (autolinking handles for React Native 0.60 +).

Instaluj Navigator

React Navigation offers multiple navigators. The head1; Xi1; FLT: 0 Xi3; Xion3; nativa stack vigator Xion1; Xion1; FLT: 1 Xion3; Xion3; is recommended for most use cases:

npm install @react-navigation/native-stack

Other nawigators like eng1; EDG1; FLT: 0 EDG3; EDG3; bottom tabs eng1; EDG1; FLT: 1 EDG3; anddig1; EDG1; FLT: 2 EDG3; EDG3; drawer EDG1; EDG1; FLT: 3 EDG3; EDG3; are installalad separately:

npm install @react-navigation/bottom-tabs
npm install @react-navigation/drawer

Core Concepts

React Navigation is built around three e main building blocks:

Every navigation setup must be wrapped in a preci1; Precidi1; FLT: 4 precidi3; Precidi3;. This container is typically placed at te root of the app containent.

Setting Up a Stack Navigator

To stack nawigator zarządza ostatnio w -pierwszym -out stack of screens. Czy to jest to nativa platform 's transition animations andd gestures by default.

First, import necessary modules:

import { NavigationContainer } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';

Stwórz stack nawigator instance:

const Stack = createNativeStackNavigator();

Definite thee nawigator inside your App content:

function App() {
 return (
 <NavigationContainer>
 <Stack.Navigator>
 <Stack.Screen name="Home" component={HomeScreen} />
 <Stack.Screen name="Details" component={DetailsScreen} />
 </Stack.Navigator>
 </NavigationContainer>
 );
}

Each previo1; Each previous 1; FLT: 8 previous 3; Evious 3; Evious 3; FLT previous a Evious 3; Evious 3; Evious 3; Prop (thee React conviolent to o render).

Screens i Navigating

Scenariusze are standard React contribuents. They receive two important props: preci1; exi1; FLT: 11 contributions 3; exibu3; and contribu1; exibu1; FLT: 12 contributes 3; exibution 13; FLT: 13 contribute; prevides methods to move between screen. Below is a typical Home screen:

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

Nie musisz tego robić, bo nie musisz tego robić.

function DetailsScreen({ route }) {
 return (
 <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
 <Text>Details Screen</Text>
 </View>
 );
}

Methods Navigation

React Navigation provides several methods via the behind 1; Xi1; FLT: 16 behind 3; Xi3; prop:

For example, to push a new instance of the the pres screen andd pass parameters:

navigation.push('Details', { itemId: 42, otherParam: 'anything' });

Parametry passing Between Screens

Data can be passed to a screen via thee second argument to o idea 1; Xi1; FLT: 18 Xi3; Xi3; Or Xi1; Xi1; FLT: 19 Xi3; Xi3;. The receiving screen accesses those parameters distrigh Xi1; FLT: 20 Xi3; Xion3; Xion3;.

// In HomeScreen
navigation.navigate('Details', { userId: 123 });

// In DetailsScreen
function DetailsScreen({ route }) {
 const { userId } = route.params;
 return <Text>User ID: {userId}</Text>;
}

Set default parameter values using preseng; Prefectu1; FLT: 22 presents 3; presenta3; on thee screen present:

<Stack.Screen
 name="Details"
 component={DetailsScreen}
 initialParams={{ itemId: 0 }}
/>

Konfiguracja Opcje Navigation

Each screen can customize it header, tab icon, drawer label, etc., using the event 1; indi1; FLT: 24 contribute 3; indisation 3; indisation 1; indisation 1; fLT: 25 contribute 3; or the event 1; indisation 1; fLT: 26 contribute 3; indisation 3; prop on thee navigator.

Headder Customization

<Stack.Screen
 name="Details"
 component={DetailsScreen}
 options={{
 title: 'Details',
 headerStyle: { backgroundColor: '#f4511e' },
 headerTintColor: '#fff',
 headerTitleStyle: { fontWeight: 'bold' },
 }}
/>

Dynamic options can be set using a function that receives indiv1; EDI1; FLT: 28 EDI3; EDI3;

options={({ route }) => ({ title: route.params.name })}

Tab Navigator

Tab nawigation is contingent for top- level app sections. Install thee bottom tabs navigator:

npm install @react-navigation/bottom-tabs

Stwórz tab nawigator i combinate it wigh your stack:

import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';

const Tab = createBottomTabNavigator();

function HomeTabs() {
 return (
 <Tab.Navigator>
 <Tab.Screen name="Feed" component={FeedScreen} />
 <Tab.Screen name="Profile" component={ProfileScreen} />
 </Tab.Navigator>
 );
}

You can nest thee tab Navigator inside a stack nawigator or vice versa. Customize tab icons using thee message 1; eng.1; FLT: 32 message 3; eng3; prop with engine 1; eng.1; FLT: 33 message 3; engy3; engy3;.

Drawer Navigator

A drawer nawigator slides a drawer frem thee left, revealing nawigation items. Install it:

npm install @react-navigation/drawer

Rozkład egzaminów:

import { createDrawerNavigator } from '@react-navigation/drawer';

const Drawer = createDrawerNavigator();

function AppDrawer() {
 return (
 <Drawer.Navigator>
 <Drawer.Screen name="Home" component={HomeScreen} />
 <Drawer.Screen name="Settings" component={SettingsScreen} />
 </Drawer.Navigator>
 );
}

Use present 1; Present 1; FLT: 36 presentation 3; Presentation 3; to open and close thee drawer programmatically.

Nested Navigators

Kompleks apps often nett nawigators (np., a tab nawigator inside a stack nawigator). React Navigation handles nesting gracefuly. The heal1; Efl1; FLT: 37 contained 3; Efl3; prop provided to any screen can call methods on a parent navigator if needed. Usie efl1; FLT: 38 contable 3; Efl3; te thee parent navigator 's methods.

Egzamin: Stack contening a tab nawigator i a settings screaen:

function RootStack() {
 return (
 <Stack.Navigator>
 <Stack.Screen name="Main" component={MainTabs} options={{ headerShown: false }} />
 <Stack.Screen name="Settings" component={SettingsScreen} />
 </Stack.Navigator>
 );
}

Linking andd Deep Linking

React Navigation supports deep linking to o handle URL s that open specific screens. Configure the indic1; indic1; FLT: 40 indic3; indic3; prop on indic1; indic1; FLT: 41 indic3; indic3;

const linking = {
 prefixes: ['myapp://', 'https://myapp.com'],
 config: {
 screens: {
 Home: 'home',
 Details: 'details/:itemId',
 },
 },
};

<NavigationContainer linking={linking}>
 {/* navigators */}
</NavigationContainer>

To jest możliwe, że to jest coś, co może być przydatne.

Autentiation Flows

Many apps require conditional navigation based on certification state. A Custon pattern is to switch the navigator at the root level:

function App() {
 const [isSignedIn, setIsSignedIn] = useState(false);

 return (
 <NavigationContainer>
 {isSignedIn ? <AppNavigator /> : <AuthNavigator />}
 </NavigationContainer>
 );
}

Alternatywne, use a single stack wigh conditional screens. Avoid flickering by keeping the sounde or state up to date.

Performance andd Beszt Practices

Konkluzja

React Navigation provides an intuitiva, customizable, and performant way tu manage navigation in React Native apps. Bymaching stack, tab, drawer, and nested navigators, along with parameter passing, deep linking, and authentiation flows, you can build robutt mobile experimenes. For further details, refer to the previden1; end 1the; FLT: 0 3Bax3; oil React Navigation documentation revident 1; FLT: 1; FLT: 1; 3and; 3and; 1anth; FLT: 3AE; FLT: 3AE; FLT; AE; Nativt; Native Navigive Navigivoun guan; FLt; FLt