InApps Technology
How to do navigation in React Native web?

How to do navigation in React Native web?

Anh HoangMay 31, 202211 min read

Setting up navigation in react native web is challenging as the navigation system works quite differently in apps vs browser.

“React Native for Web” makes it possible to run React Native components and APIs on the web using React DOM. It is as simple as that!! So, if you have an app and you want to build a web version of it, or if you are planning to build a website along with an app, you should definitely learn React native web.

Setting up navigation in react native web is challenging as the navigation system works quite differently in apps vs browsers.

In this article, we’ll set up the most popular react-navigation on react-native-web.

Key Summary

This article provides a detailed guide on integrating react-navigation into a React Native Web application using Expo, enabling seamless navigation across mobile (iOS/Android) and web platforms. It addresses the challenge of differing navigation systems between apps and browsers, ensuring URL updates reflect screen changes on the web. Key points include:

Overview: React Native for Web allows running React Native components on the web via React DOM, ideal for building web versions of apps or simultaneous app/website development. react-navigation is the leading navigation library for React Native, adaptable for web with additional configuration.
React Native for Web allows running React Native components on the web via React DOM, ideal for building web versions of apps or simultaneous app/website development.
react-navigation is the leading navigation library for React Native, adaptable for web with additional configuration.
Prerequisites: Use Expo as the development platform. Install expo-cli and clone the provided GitHub repository (master branch) or use the react-navigation-setup branch for quick setup. Run expo start to initialize the project.
Use Expo as the development platform.
Install expo-cli and clone the provided GitHub repository (master branch) or use the react-navigation-setup branch for quick setup.
Run expo start to initialize the project.
Installation:
Install required packages: bash CollapseWrapRun Copy expo install react-navigation react-native-gesture-handler react-native-reanimated react-native-screens
npm i react-navigation-stack @react-navigation/web
Ensure react-navigation version is 4+ in package.json.
Expo ensures compatibility with the installed Expo version, using npm internally.
Setup Screens and Navigation: Create Screens: Feed Screen: Displays text and a button to navigate to the Profile screen. Profile Screen: Similar to Feed, with a button to navigate to Feed. Both use StyleSheet for centered layout and Button for navigation via this.props.navigation.navigate. Stack Navigation (Home.js): Use createStackNavigator to connect Feed and Profile screens. Set Profile as the default screen (first in the navigator object). Configure navigationOptions for header styling (e.g., black background, white text). Use createAppContainer to wrap the navigator. App.js: Render the Home navigator within a styled View.
Create Screens: Feed Screen: Displays text and a button to navigate to the Profile screen. Profile Screen: Similar to Feed, with a button to navigate to Feed. Both use StyleSheet for centered layout and Button for navigation via this.props.navigation.navigate.
Feed Screen: Displays text and a button to navigate to the Profile screen.
Profile Screen: Similar to Feed, with a button to navigate to Feed.
Both use StyleSheet for centered layout and Button for navigation via this.props.navigation.navigate.
Stack Navigation (Home.js): Use createStackNavigator to connect Feed and Profile screens. Set Profile as the default screen (first in the navigator object). Configure navigationOptions for header styling (e.g., black background, white text). Use createAppContainer to wrap the navigator.
Use createStackNavigator to connect Feed and Profile screens.
Set Profile as the default screen (first in the navigator object).
Configure navigationOptions for header styling (e.g., black background, white text).
Use createAppContainer to wrap the navigator.
App.js: Render the Home navigator within a styled View.
Render the Home navigator within a styled View.
Running the App: Launch with expo start. Press i for iOS simulator (displays Profile screen; button navigates to Feed). Press w for web browser (same functionality, but URL updates need further configuration).
Launch with expo start.
Press i for iOS simulator (displays Profile screen; button navigates to Feed).
Press w for web browser (same functionality, but URL updates need further configuration).
Web-Specific Navigation: Issue: Default setup doesn’t update browser URLs when navigating (e.g., clicking “Go to Feed” doesn’t change URL). Solution: Add static path to screens: Feed: static path = “feed”; Profile: static path = “”; (default route). Direct URL access (e.g., http://localhost:19006/feed) renders the Feed screen.
Issue: Default setup doesn’t update browser URLs when navigating (e.g., clicking “Go to Feed” doesn’t change URL).
Solution: Add static path to screens: Feed: static path = “feed”; Profile: static path = “”; (default route). Direct URL access (e.g., http://localhost:19006/feed) renders the Feed screen.
Add static path to screens: Feed: static path = “feed”; Profile: static path = “”; (default route).
Feed: static path = “feed”;
Profile: static path = “”; (default route).
Direct URL access (e.g., http://localhost:19006/feed) renders the Feed screen.
Replace Button with @react-navigation/web’s Link for web: javascript CollapseWrapRun Copy import { Link } from “@react-navigation/web”;

const isWeb = Platform.OS === ‘web’;

// Conditionally render

!isWeb ?
Use createBrowserApp instead of createAppContainer for web: javascript CollapseWrapRun Copy const isWeb = Platform.OS === ‘web’;
const container = isWeb ? createBrowserApp(Home) : createAppContainer(Home);
Result: Clicking navigation buttons updates URLs (e.g., http://localhost:19006/feed), and direct URL entry works.
Additional Features: Add navigationOptions with title (e.g., title: “Feed”) to display screen titles in the header. The Link module converts to tags on the web, enhancing browser compatibility. Platform.OS ensures platform-specific rendering (Button for apps, Link for web).

Using React navigation in react native web

React navigation is the most famous library for navigation in react–native. In this section, we’ll try to integrate react-navigation in react–native–web.

Prerequisite

To set up your codebase using expo, check this GitHub link [master branch]. Simply clone the branch and do run expo start

If you are looking for a quick code, then you can check this GitHub link [NOTE: the branch is react–navigation–setup and not master.]

Installation

Run the following command to install react–navigation along with other needed packages including react–navigation–stack.

expo install react-navigation react-native-gesture-handler react-native-reanimated react-native-screens npm i react-navigation-stack npm i @react-navigation/web

Check your package.json files to ensure all of the above packages are installed, make sure react–navigation is version 4+. If you are wondering, why did we use expo instead of NPM/yarn while installing react–navigation, then the reason is that expo would look for the correct version of the react–navigation libraries that’d work with the expo version that’s installed in your project.

If you look into the printed console it uses NPM underneath.

Create a few screens

Now, let’s set up a few screens to test our navigation flow:

// feed screen import React from 'react'; import {View, Text, StyleSheet, Button, Platform} from 'react-native'; export default class Feed extends React.Component { render() { return This is the feed screen