Convert Figma logo to code with AI

react-navigation logoreact-navigation

Routing and navigation for your React Native apps

23,750
5,060
23,750
779

Top Related Projects

Native navigation primitives for your React Native app.

The first declarative React Native router

Routing and navigation for React Native and Web apps

Quick Overview

React Navigation is a popular routing and navigation library for React Native applications. It provides a flexible and customizable way to handle navigation between screens, manage navigation state, and create various navigation patterns such as stack, tab, and drawer navigation.

Pros

  • Easy to use and integrate with React Native projects
  • Highly customizable with a wide range of navigation options
  • Supports deep linking and web browser-like navigation
  • Active community and regular updates

Cons

  • Can be complex for beginners or simple navigation needs
  • Performance issues may arise with deeply nested navigation structures
  • Some advanced features require additional setup or third-party libraries
  • Documentation can be overwhelming due to the library's extensive features

Code Examples

  1. Creating a stack navigator:
import { createStackNavigator } from '@react-navigation/stack';

const Stack = createStackNavigator();

function MyStack() {
  return (
    <Stack.Navigator>
      <Stack.Screen name="Home" component={HomeScreen} />
      <Stack.Screen name="Details" component={DetailsScreen} />
    </Stack.Navigator>
  );
}
  1. Navigating between screens:
import { useNavigation } from '@react-navigation/native';

function HomeScreen() {
  const navigation = useNavigation();

  return (
    <Button
      title="Go to Details"
      onPress={() => navigation.navigate('Details')}
    />
  );
}
  1. Passing parameters to a screen:
navigation.navigate('Details', { itemId: 86, otherParam: 'anything' });

// In the Details screen:
function DetailsScreen({ route }) {
  const { itemId, otherParam } = route.params;
  // Use the parameters in your component
}

Getting Started

  1. Install the required packages:
npm install @react-navigation/native
npm install react-native-screens react-native-safe-area-context
  1. Wrap your app with NavigationContainer:
import { NavigationContainer } from '@react-navigation/native';

export default function App() {
  return (
    <NavigationContainer>
      {/* Your app content */}
    </NavigationContainer>
  );
}
  1. Create a navigator (e.g., stack navigator) and define your screens:
import { createStackNavigator } from '@react-navigation/stack';

const Stack = createStackNavigator();

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

Competitor Comparisons

Native navigation primitives for your React Native app.

Pros of react-native-screens

  • Optimized performance for native navigation
  • Reduced memory usage and improved app stability
  • Seamless integration with React Native's native modules

Cons of react-native-screens

  • Limited built-in navigation patterns compared to React Navigation
  • Steeper learning curve for developers new to native navigation concepts
  • May require additional configuration for complex navigation scenarios

Code Comparison

React Navigation:

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

const Stack = createStackNavigator();

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

react-native-screens:

import { Screen, ScreenContainer } from 'react-native-screens';

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

React Navigation provides a more declarative API for defining navigation structure, while react-native-screens offers a lower-level approach that can be integrated with various navigation libraries or custom solutions.

The first declarative React Native router

Pros of react-native-router-flux

  • Simpler API with less boilerplate code
  • Easier to implement complex navigation patterns
  • Better performance for deep navigation stacks

Cons of react-native-router-flux

  • Less active development and community support
  • Fewer built-in navigation components and options
  • Limited documentation and examples

Code Comparison

react-native-router-flux:

<Router>
  <Scene key="root">
    <Scene key="home" component={Home} initial={true} />
    <Scene key="profile" component={Profile} />
  </Scene>
</Router>

react-navigation:

const Stack = createStackNavigator();

<NavigationContainer>
  <Stack.Navigator>
    <Stack.Screen name="Home" component={Home} />
    <Stack.Screen name="Profile" component={Profile} />
  </Stack.Navigator>
</NavigationContainer>

Summary

React Native Router Flux offers a simpler API and easier implementation of complex navigation patterns, making it attractive for developers who prefer a more straightforward approach. However, React Navigation has more active development, better documentation, and a wider range of built-in components. React Navigation is generally considered the more robust and future-proof option, especially for larger projects or those requiring extensive customization.

The choice between the two libraries depends on project requirements, team preferences, and the need for long-term support and updates. React Navigation is often recommended for new projects due to its ongoing development and extensive ecosystem.

Routing and navigation for React Native and Web apps

Pros of react-navigation

  • More comprehensive documentation and community support
  • Wider range of navigation options and customization features
  • Better integration with React Native's native components

Cons of react-navigation

  • Steeper learning curve for beginners
  • Larger bundle size due to more features
  • May require additional configuration for complex navigation scenarios

Code Comparison

react-navigation:

import { createStackNavigator } from '@react-navigation/stack';

const Stack = createStackNavigator();

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

react-navigation:

import { StackNavigator } from 'react-navigation';

const App = StackNavigator({
  Home: { screen: HomeScreen },
  Details: { screen: DetailsScreen },
});

The code comparison shows that react-navigation uses a more declarative approach with the NavigationContainer and Stack.Navigator components, while react-navigation uses a configuration object to define the navigation structure. react-navigation's syntax is more modern and aligns better with React's component-based architecture.

Both libraries provide similar functionality, but react-navigation offers more flexibility and features. However, react-navigation may be simpler for basic navigation needs and could be easier for beginners to grasp initially.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

React Navigation 7

Build Status Code Coverage MIT License

Routing and navigation for your React Native apps.

Documentation can be found at reactnavigation.org.

This branch contains the code for the latest stable version of React Navigation. You can find the code for previous versions in the following branches:

Package Versions

NameLatest Version
@react-navigation/bottom-tabsbadge
@react-navigation/corebadge
@react-navigation/devtoolsbadge
@react-navigation/drawerbadge
@react-navigation/elementsbadge
@react-navigation/material-top-tabsbadge
@react-navigation/native-stackbadge
@react-navigation/nativebadge
@react-navigation/routersbadge
@react-navigation/stackbadge
react-native-drawer-layoutbadge
react-native-tab-viewbadge

Contributing

Please read through our contribution guide to get started!

Installing from a fork on GitHub

Since we use a monorepo, it's not possible to install a package from the repository URL. If you need to install a forked version from Git, you can use gitpkg.

First install gitpkg:

yarn global add gitpkg

Then follow these steps to publish and install a forked package:

  1. Fork this repo to your account and clone the forked repo to your local machine
  2. Open a Terminal and cd to the location of the cloned repo
  3. Run yarn to install any dependencies
  4. If you want to make any changes, make them and commit
  5. Run yarn lerna run prepack to perform the build steps
  6. Now cd to the package directory that you want to use (e.g. cd packages/stack for @react-navigation/stack)
  7. Run gitpkg publish to publish the package to your repo

After publishing, you should see something like this:

Package uploaded to git@github.com:<user>/<repo>.git with the name <name>

You can now install the dependency in your project:

yarn add <user>/<repo>.git#<name>

Remember to replace <user>, <repo> and <name> with right values.

NPM DownloadsLast 30 Days