Reader small image

You're reading from  React Router Quick Start Guide

Product typeBook
Published inSep 2018
Reading LevelIntermediate
PublisherPackt
ISBN-139781789532555
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Sagar Ganatra
Sagar Ganatra
author image
Sagar Ganatra

Sagar Ganatra is a frontend engineer and an architect from Bangalore, India. He has more than a decade of experience in developing web and mobile applications. He specializes in architecting projects using JavaScript and frameworks such as React, Angular, and Node. His previous books include Kendo UI Cookbook and Instant Kendo UI Mobile, both published by Packt Publishing. He also writes about frontend technologies in his blog, sagarganatra (dot) com.
Read more about Sagar Ganatra

Right arrow

Using NativeRouter in a React Native Application

The React Router library provides the react-router-native package, which includes the implementation of the NativeRouter component for use in React Native applications. The React Native framework allows you to build native mobile applications for iOS and Android using JavaScript and React.

From React Native's Documentation (https://facebook.github.io/react-native/):

"With React Native, you don't build a mobile web app, an HTML5 app, or a hybrid app. You build a real mobile app that's indistinguishable from an app built using Objective-C or Java. React Native uses the same fundamental UI building blocks as regular iOS and Android apps. You just put those building blocks together using JavaScript and React."

In this chapter, the following topics are discussed:

  • Using NativeRouter in a React Native application...

Using NativeRouter in a React Native application

Similar to the create-react-app CLI, the create-react-native-app CLI is used to create an application that includes build scripts that can be used to build an application for both development and production environments. It also includes packager, which allows you to test your application on iOS and Android emulators and also on real devices.

Creating a new project with the create-react-native-app CLI

Let's get started by first installing the CLI:

npm install -g create-react-native-app

The preceding command installs the CLI in the global node_modules directory. The next step is to create a React Native project using the CLI:

create-react-native-app react-native-test-app...

The <NativeRouter> component

The NativeRouter component uses the MemoryRouter component defined in the react-router package to provide routing support in a React Native application. MemoryRouter is used when you want to maintain the browsing history in memory without updating the URL in the address bar. It's particularly useful in non-browser environments where an address bar is not available. The MemoryRouter component creates a history object using the createMemoryHistory class available in the history package. This history object is then provided to the low-level <Router> interface.

In NativeRotuer.js:

import MemoryRouter from "react-router/MemoryRouter";

const NativeRouter = props => <MemoryRouter {...props} />;

Then the MemoryRouter component creates a history object using createMemoryHistory, in MemoryRouter.js:

import { createMemoryHistory...

The <BackButton> component

By default, when you press the back button on an Android device, the application exits instead of navigating the user to the previous state in the history. The React Native library includes a BackHandler class, which lets you customize the behavior of the devices' hardware back button. The <BackButton> component in React Router uses the BackHandler class to customize the behavior of the back button on an Android device:

import { NativeRouter, BackButton } from 'react-router-native';

export default class App extends Component {
render() {
return (
<NativeRouter>
<View style={styles.container}>
<BackButton />
<SideMenu menu={menu}>
<ContentView />
</SideMenu>
<...

Summary

In this chapter, we looked at how React Router's <NativeRouter> component can be used in a React Native application. The <NativeRouter> component is available in the react-router-native package, and it uses the <MemoryRouter> component internally, which is defined in the react-router package. The <NativeRouter> component accepts props: initialEntries, initialIndex, getUserConfirmation, keyLength, and children. Also, it provides a default implementation for the getUserConfirmation function, which uses React Native's Alert component to display a confirmation message. This confirmation message is shown when the <Prompt> component is included in the application and the user tries to navigate away from the current route.

The <BackButton> component in react-router-native is a wrapper around React Native's BackHandler class,...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
React Router Quick Start Guide
Published in: Sep 2018Publisher: PacktISBN-13: 9781789532555
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
Sagar Ganatra

Sagar Ganatra is a frontend engineer and an architect from Bangalore, India. He has more than a decade of experience in developing web and mobile applications. He specializes in architecting projects using JavaScript and frameworks such as React, Angular, and Node. His previous books include Kendo UI Cookbook and Instant Kendo UI Mobile, both published by Packt Publishing. He also writes about frontend technologies in his blog, sagarganatra (dot) com.
Read more about Sagar Ganatra