Set up navigation in The React Native Web application for Beginners

Set up navigation in The React Native Web application for Beginners

In this article we will talk about React navigation which is the most famous library for navigating in native React. We will try to integrate reaction navigation in Google's response web.

Of the several navigation libraries out there, React Navigation and React Native Navigation are two of the more well known.

React Navigation is written in JavaScript and does not directly use the native navigation APIs on iOS and Android; rather, it recreates some subset of those APIs. This allows for integration of third-party JS plugins, maximum customization, and easier debugging, with no need to learn Objective-C, Swift, Java, Kotlin, etc.

React Native Navigation differs slightly in that it directly uses native navigation APIs on iOS and Android, which allows for a more native look and feel.

In this article we will talk about React navigation which is the most famous library for navigating in native React. We will try to integrate reaction navigation in Google's response web.

Note : 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.

Installation

the first step is to set up a React Native app. The easiest way to get started with React Native is with Expo tools because they allow you to start a project without installing and configuring Xcode or Android Studio. Install Expo by running this:

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:

  • Profile screen

  • Post screen

// feed screen
 
import React from 'react';
import {View, Text, StyleSheet, Button, Platform} from 'react-native';
 
export default class Feed extends React.Component {
 
    render() {
        return <View style={styles.container}>
            <Text>This is the feed screen</Text>
           <Button
                    title="Go to Profile"
                    onPress={() => this.props.navigation.navigate('Profile')}
            />
 
 
        </View>
    }
}
 
const styles = StyleSheet.create({
    container: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
    }
})

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


// Profile screen
 
import React from 'react';
import {View, Text, StyleSheet, Button, Platform} from 'react-native';
 
export default class Profile extends React.Component {
 
    render() {
        return <View style={styles.container}>
            <Text>This is the profile screen</Text>
            <Button
                title="Go to Feed"
                onPress={() => this.props.navigation.navigate('Feed')}
            />
 
        </View>
    }
}
 
const styles = StyleSheet.create({
    container: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
    }
})

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:


// Home.js, just a name of the stack navigation
 
import {createStackNavigator} from 'react-navigation-stack';
import {createAppContainer} from 'react-navigation';
 
import Feed from "../screens/Feed";
import Profile from "../screens/Profile";
 
 
const Home = createStackNavigator(
    {
        Profile: Profile,
        Feed: Feed,
    },
    {
        navigationOptions: {
            headerTintColor: '#fff',
            headerStyle: {
                backgroundColor: '#000',
            },
        },
    }
);
 
const container = createAppContainer(Home);
 
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.


// App.js
 
export default function App() {
  return (
    <View style={styles.outer}>
      <Home/>
    </View>
  );
}

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.

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.

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:

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

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.

export default class Feed extends React.Component {
    static navigationOptions = {
        title: "Feed"
    }
 
    static path = "feed";
 
    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.


// feed screen
 
import React from 'react';
import {View, Text, StyleSheet, Button, Platform} from 'react-native';
import {Link} from "@react-navigation/web";
 
const isWeb = Platform.OS === 'web';
 
 
export default class Feed extends React.Component {
    static navigationOptions = {
        title: "Feed"
    }
 
    static path = "feed";
 
    render() {
        return <View style={styles.container}>
            <Text>This is the feed screen</Text>
            {
                !isWeb ? <Button
                    title="Go to Profile"
                    onPress={() => this.props.navigation.navigate('Profile')}
            />:  <Link routeName="Profile">Go Profile</Link>
 
            }
 
 
        </View>
    }
}
 
const styles = StyleSheet.create({
    container: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
    }
})


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:


// Home.js, just a name of the stack navigation
 
import {createStackNavigator} from 'react-navigation-stack';
import {createAppContainer} from 'react-navigation';
import {createBrowserApp} from '@react-navigation/web';
 
import Feed from "../screens/Feed";
import Profile from "../screens/Profile";
import {Platform} from "react-native";
 
const isWeb = Platform.OS === 'web';
 
 
const Home = createStackNavigator(
    {
        Profile: Profile,
        Feed: Feed,
    },
    {
        navigationOptions: {
            headerTintColor: '#fff',
            headerStyle: {
                backgroundColor: '#000',
            },
        },
    }
);
 
const container = isWeb ? createBrowserApp(Home): createAppContainer(Home);
 
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.

Conclusion

I hope that this article will jumpstart your use of the React Navigation package in your existing or future React Native projects. There’s a lot more that can be done; most of your needs will be met by this package. Feel free to explore more of the documentation and to grab the final code GitHub repo.

Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

What’s new in HTML6

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

How to Share Code Between React and React Native

How to Share Code Between React and React Native

Learn how to share code between React and React Native to avoid duplicating logic. Sharing Code Between React and React-Native: What Not to Share. This question of sharing code between React and React Native, in fact, one of the hot topics among React and React native developers all over the world. React and React-Native allow a learn once write anywhere paradigm. This is great, because one tech team can build both your web app and native mobile experience. The problem is developers hate writing things twice. There have been a couple of efforts to build a unifying technology to write an application once and have it work on both web and native.

How to Share Code Between React and React Native

Sharing Code Between React and React-Native: What Not to Share - Ben Ellerby

React and React-Native allow a learn once write anywhere paradigm. This is great, because one tech team can build both your web app and native mobile experience. The problem is developers hate writing things twice. There have been a couple of efforts to build a unifying technology to write an application once and have it work on both web and native. Yet this is not always the best approach. There is value in only sharing your business and state logic; keeping your render code separate.

In this talk I will give real examples from my work with MADE.COM, migrating their web and mobile application to React and React-Native with code sharing as a primary objective.

How to Share Code Between React and React Native

Learn how to share code between React and React Native to avoid duplicating logic

Sharing Code Between React and React Native

React and React-Native allow a learn once write anywhere paradigm. This is great, because one tech team can build both your web app and native mobile experience. The problem is developers hate writing things twice. There have been a couple of efforts to build a unifying technology to write an application once and have it work on both web and native. Yet this is not always the best approach. There is value in only sharing your business and state logic; keeping your render code separate.

React Native vs Flutter | Difference Between React Native & Flutter

React Native vs Flutter | Difference Between React Native & Flutter

This video on React Interview Questions will help you crack your next React interview with ease. React is the most popular front-end JavaScript library today and is being adopted by many big companies like Netflix, Airbnb, New York Times, and many more...

This video on React Interview Questions will help you crack your next React interview with ease. React is the most popular front-end JavaScript library today and is being adopted by many big companies like Netflix, Airbnb, New York Times, and many more. It is also used in small projects by web developers to showcase their skills in the field of web development. The video includes general React interview questions as well as questions focused on Redux, Hooks, and Styling. This video is ideal for both beginners as well as experienced professionals who are appearing for React web development job interviews. Learn the most important React interview questions and answers and know what will set you apart in the interview process.

React Native Tutorial : Build Dota 2 fan app in React Native

React Native Tutorial : Build Dota 2 fan app in React Native

In this video I'll talk about my approach on how I built a react native app, that I've been working on as a side project for ~1 week, for ios and android and what it can do.

In this video I'll talk about my approach on how I built a react native app, that I've been working on as a side project for ~1 week, for ios and android and what it can do.

The idea for the app was basically to help dota fans quickly browse vods of ongoing/previous tournaments, check upcoming matches, read news and play a little prediction mini game.

In the video I explain why I decided not to publish it.

I used a bunch of web scraping(cheerio) from my node backend to get the content needed for the app and a mongo database to store the data.