DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • React, Angular, and Vue.js: What’s the Technical Difference?
  • How to Build Scalable Mobile Apps With React Native: A Step-by-Step Guide
  • Mastering React App Configuration With Webpack
  • How to Build a React Native Chat App for Android

Trending

  • Integration Isn’t a Task — It’s an Architectural Discipline
  • AI-Driven Root Cause Analysis in SRE: Enhancing Incident Resolution
  • How to Build Real-Time BI Systems: Architecture, Code, and Best Practices
  • Designing a Java Connector for Software Integrations
  1. DZone
  2. Coding
  3. JavaScript
  4. Navigation in a React Native Web Application

Navigation in a React Native Web Application

By 
Jason Rees user avatar
Jason Rees
·
Feb. 05, 20 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
11.0K Views

Join the DZone community and get the full member experience.

Join For Free

“React Native for Web” makes it possible to run React Native components and APIs on the web using React DOM.

Setting up navigation in react–native–web is challenging as the navigation system works quite differently in  apps vs  browser. In this article, we’ll set up the most popular react-navigation on to react-native-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

For this setup, we’ll be using expo as the platform on which we’ll be building our React Native app, which will run on Android, iOS, and the web. Make sure you have expo-cli installed already.

To set up your codebase using expo, check this GitHub link [master branch]. Simply clone the branch and 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).

You may also like: A Look at React Native and React.js.

Installation

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

Shell
 




x


 
1
expo install react-navigation react-native-gesture-handler react-native-reanimated react-native-screens
2
 
3
npm i react-navigation-stack
4
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.

installing necessary dependencies

Installing necessary dependencies


Create a Few Screens

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

  • Profile screen
  • Post screen
JavaScript
 




xxxxxxxxxx
1
27


 
1
// feed screen
2
 
3
import React from 'react';
4
import {View, Text, StyleSheet, Button, Platform} from 'react-native';
5
 
6
export default class Feed extends React.Component {
7
 
8
    render() {
9
        return <View style={styles.container}>
10
            <Text>This is the feed screen</Text>
11
           <Button
12
                    title="Go to Profile"
13
                    onPress={() => this.props.navigation.navigate('Profile')}
14
            />
15
 
16
 
17
        </View>
18
    }
19
}
20
 
21
const styles = StyleSheet.create({
22
    container: {
23
        flex: 1,
24
        justifyContent: 'center',
25
        alignItems: 'center',
26
    }
27
})



The above is the code for FeedScreen, which is simple text and a button. The button when clicked should go directly to the profile screen.

JavaScript
 




xxxxxxxxxx
1
26


 
1
// Profile screen
2
 
3
import React from 'react';
4
import {View, Text, StyleSheet, Button, Platform} from 'react-native';
5
 
6
export default class Profile extends React.Component {
7
 
8
    render() {
9
        return <View style={styles.container}>
10
            <Text>This is the profile screen</Text>
11
            <Button
12
                title="Go to Feed"
13
                onPress={() => this.props.navigation.navigate('Feed')}
14
            />
15
 
16
        </View>
17
    }
18
}
19
 
20
const styles = StyleSheet.create({
21
    container: {
22
        flex: 1,
23
        justifyContent: 'center',
24
        alignItems: 'center',
25
    }
26
})


The Profile screen is the same as the Feed screen. Both the screens have a button which takes to the other screen.

Let’s also create stack navigation to connect these two screens together:

JavaScript
 




xxxxxxxxxx
1
27


 
1
// Home.js, just a name of the stack navigation
2
 
3
import {createStackNavigator} from 'react-navigation-stack';
4
import {createAppContainer} from 'react-navigation';
5
 
6
import Feed from "../screens/Feed";
7
import Profile from "../screens/Profile";
8
 
9
 
10
const Home = createStackNavigator(
11
    {
12
        Profile: Profile,
13
        Feed: Feed,
14
    },
15
    {
16
        navigationOptions: {
17
            headerTintColor: '#fff',
18
            headerStyle: {
19
                backgroundColor: '#000',
20
            },
21
        },
22
    }
23
);
24
 
25
const container = createAppContainer(Home);
26
 
27
export default container;



Since the object that’s passed to CreateStackNavigator Profile comes first, the Profile screen is the default screen of this stack navigator.

Now, in the App.js file, simply render the Home Navigation.

JavaScript
 




xxxxxxxxxx
1


 
1
// App.js
2
 
3
export default function App() {
4
  return (
5
    <View style={styles.outer}>
6
      <Home></Home>
7
    </View>
8
  );
9
}



Just run the app using command, expo start, and it should launch the expo bundler for you.

If you press i to launch the Expo app in the iOS simulator in the terminal, the following screen comes up on the display, assuming everything goes well.

Profile screen demo

Profile screen demo


When you click on the Go to Feed button, it should take you to the feed screen.

To run the same setup on to the web, simply press w in your terminal. It will launch the web app in your default browser.

application running in web

Application running in web


The click functionality also works on the web as well. The top border is for the screen title, you can add it by adding navigationOptions to the feed and profile screen like this:

JavaScript
 




xxxxxxxxxx
1


 
1
export default class Feed extends React.Component {
2
    static navigationOptions = {
3
        title: "Feed"
4
    }



But, there is one problem with the web navigation, the URL doesn’t change when you go from the profile to the feed screen. In the web navigation, it is extremely important to have the change in page reflecting in the URL as well.

In the app, there is no way a user can directly jump to a screen other than the default screen, but in the browser, it is possible; a user can enter a URL.

The good part of react–navigation is that it supports URL updates when the screen changes. The way navigationOptions is added inside the screen class, you can also add a title.

JavaScript
 




xxxxxxxxxx
1


 
1
export default class Feed extends React.Component {
2
    static navigationOptions = {
3
        title: "Feed"
4
    }
5
 
6
    static path = "feed";
7
 
8
    render() {



For the profile screen, you can keep the path as empty static path = "".

When you go to http://localhost:19006/feed, the web app would understand that you want to go to the feed screen and will render that for you. Try going to http://localhost:19006/feed directly, and it should render the feed page for you. But when you click on the  Go to Feed  button, the URL won’t change.

There are a few other things that we need to do to make this work:
@react-navigation/web also provides a Link module that gets converted into an a tag on to the web.

This module doesn’t work when you try to run the app. So, we use Platform module provided by react–native to differentiate between web and app.

JavaScript
 




xxxxxxxxxx
1
39


 
1
// feed screen
2
 
3
import React from 'react';
4
import {View, Text, StyleSheet, Button, Platform} from 'react-native';
5
import {Link} from "@react-navigation/web";
6
 
7
const isWeb = Platform.OS === 'web';
8
 
9
 
10
export default class Feed extends React.Component {
11
    static navigationOptions = {
12
        title: "Feed"
13
    }
14
 
15
    static path = "feed";
16
 
17
    render() {
18
        return <View style={styles.container}>
19
            <Text>This is the feed screen</Text>
20
            {
21
                !isWeb ? <Button
22
                    title="Go to Profile"
23
                    onPress={() => this.props.navigation.navigate('Profile')}
24
            />:  <Link routeName="Profile">Go Profile</Link>
25
 
26
            }
27
 
28
 
29
        </View>
30
    }
31
}
32
 
33
const styles = StyleSheet.create({
34
    container: {
35
        flex: 1,
36
        justifyContent: 'center',
37
        alignItems: 'center',
38
    }
39
})



Here, we are conditionally rendering the Link and the Button.

You need to do similar changes for the Profile screen as well. Also, in the navigation container, instead of createAppContainer, you need to use createBrowserApp for the web.

Here is the code for the navigation:

JavaScript
 




xxxxxxxxxx
1
31


 
1
// Home.js, just a name of the stack navigation
2
 
3
import {createStackNavigator} from 'react-navigation-stack';
4
import {createAppContainer} from 'react-navigation';
5
import {createBrowserApp} from '@react-navigation/web';
6
 
7
import Feed from "../screens/Feed";
8
import Profile from "../screens/Profile";
9
import {Platform} from "react-native";
10
 
11
const isWeb = Platform.OS === 'web';
12
 
13
 
14
const Home = createStackNavigator(
15
    {
16
        Profile: Profile,
17
        Feed: Feed,
18
    },
19
    {
20
        navigationOptions: {
21
            headerTintColor: '#fff',
22
            headerStyle: {
23
                backgroundColor: '#000',
24
            },
25
        },
26
    }
27
);
28
 
29
const container = isWeb ? createBrowserApp(Home): createAppContainer(Home);
30
 
31
export default container;



Try running the app in the browser. The Go to feed button click should change the URL to http://localhost:19006/feed.

Also, the app should be running fine on the simulators as well.



Further Reading

  • 7 Reasons Why React Native Is So Popular.
React Native Web application React (JavaScript library) app Profile (engineering) JavaScript

Opinions expressed by DZone contributors are their own.

Related

  • React, Angular, and Vue.js: What’s the Technical Difference?
  • How to Build Scalable Mobile Apps With React Native: A Step-by-Step Guide
  • Mastering React App Configuration With Webpack
  • How to Build a React Native Chat App for Android

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!