ReactPWA

ReactPWA

  • Docs
  • Features
  • Blog
  • Examples
  • Help

›Routing

Getting Started

  • Initial setup
  • PawJS Configuration
  • Web App manifest

Guide

  • External resources
  • Internal APIs
  • Environment Variables

Routing

  • Add new route
  • Async routing
  • Nested routes

Plugins

  • Redux
  • Image Optimization
  • Sass
  • LESS

Deployments

  • Heroku
  • Amazon EC2
  • Digital Ocean
Edit

Add new route

What is a Router?

A router allows your application to navigate between different components, changing the browser URL, modifying the browser history, and keeping the UI state in sync. However, as React focuses only on building user interfaces, it doesn’t have a built-in solution for routing.

React Router is the most popular routing library for React.

Configuring Route

Lets consider adding a page /contact-us to our PWA app.

Step 1: Create a new file src/pages/contact.js

Using normal import

import ContactUs from "../app/components/contact-us";
export default [
  {
    //path that user will access
    path: "/contact-us", 
    exact: true,          
    //Component to be displayed when the user visits the url of path
    component: ContactUs, 
  }
];

OR

Using dynamic import()

export default [
  // ... other routes
  {
    path: "/contact-us",
    exact: true,
    component: import("../app/components/contact-us"),
  }
];

Step 2: Add file to the routes

We need to add the new route to the list of existing routes.
In routes.js :

// other imports
import ContactUsRoute from "./pages/contact-us";

export default class Routes {
  
  apply(routeHandler) {
  
    const routes = [
      // ... other routes
      ...ContactUsRoute
    ];
    
    routeHandler.hooks.initRoutes.tapPromise("AppRoutes", async () => {
      routeHandler.addRoutes(routes);
    });
  }
}

We use React-Router v4 along with some extra options.

← Environment VariablesAsync routing →
  • What is a Router?
  • Configuring Route
    • Step 1: Create a new file src/pages/contact.js
    • Step 2: Add file to the routes
ReactPWA
Docs
Getting StartedPluginsExamples
Community
User ShowcaseProject Chat
More
BlogGitHubStar

Giants supporting us

Atyantik Technologies Private LimitedDigitalOceanEventerpriseBrowserStackNavicat
 
Support ReactPWA

Copyright © 2019 Atyantik Technologies Private Limited