react-navigation

https://reactnavigation.org/docs/hello-react-navigation/

中文文档

https://juejin.cn/post/6850418114379644942

基本

  1. import { NavigationContainer } from '@react-navigation/native';
  2. import { createStackNavigator } from '@react-navigation/stack';

Header buttons

Configuring the header bar

Passing parameters to routes

Moving between screens

hello-react-navigation

Glossary

Navigator
Screen
Route
Navigation Prop
Route Prop
Header

Guides

Tab navigation
Drawer navigation
Authentication
Safe areas
Hiding tab bar in specific screens
Different status bar configuration
Custom Android back button behavior
Preventing going back
Navigating without the navigation prop
Screen tracking for analytics
Themes

API Reference

NavigationContainer

The NavigationContainer is responsible for managing your app state and linking your top-level navigator to the app environment.

管理应用状态和链接顶级导航器到应用程序环境

  1. import { NavigationContainer } from '@react-navigation/native';
  2. import { createStackNavigator } from '@react-navigation/stack';
  3. const Stack = createStackNavigator();
  4. export default function App() {
  5. return (
  6. <NavigationContainer>
  7. <Stack.Navigator>{/* ... */}</Stack.Navigator>
  8. </NavigationContainer>
  9. );
  10. }

Stack.Screen

Navigation prop

Navigation events

  1. function Profile({ navigation }) {
  2. React.useEffect(() => {
  3. const unsubscribe = navigation.addListener('focus', () => {
  4. // do something
  5. });
  6. return unsubscribe;
  7. }, [navigation]);
  8. return <ProfileContent />;
  9. }
  10. // 2
  11. <Tab.Screen
  12. name="Chat"
  13. component={Chat}
  14. listeners={{
  15. tabPress: e => {
  16. // Prevent default action
  17. e.preventDefault();
  18. },
  19. }}
  20. />

Navigators

createBottomTabNavigator
createDrawerNavigator
createNativeStackNavigator
createStackNavigator

  1. const Stack = createStackNavigator<RootStackParamList>();
  2. <Stack.Navigator screenOptions={{ headerShown: false }}>
  3. <Stack.Screen name="Home" component={HomeScreen} />
  4. {/* <Stack.Screen name="Root" component={BottomTabNavigator} /> */}
  5. <Stack.Screen
  6. name="NotFound"
  7. component={NotFoundScreen}
  8. options={{ title: "Oops!" }}
  9. />
  10. </Stack.Navigator>

Hooks

useNavigation
useScrollToTop

  1. import * as React from 'react';
  2. import { Button } from 'react-native';
  3. import { useNavigation } from '@react-navigation/native';
  4. function MyBackButton() {
  5. const navigation = useNavigation();
  6. return (
  7. <Button
  8. title="Back"
  9. onPress={() => {
  10. navigation.goBack();
  11. }}
  12. />
  13. );
  14. }

Actions

example

https://github.com/react-navigation/react-navigation/tree/main/example