React Router Quick Start Guide
eBook - ePub

React Router Quick Start Guide

Routing in React applications made easy

Sagar Ganatra

  1. 156 pagine
  2. English
  3. ePUB (disponibile sull'app)
  4. Disponibile su iOS e Android
eBook - ePub

React Router Quick Start Guide

Routing in React applications made easy

Sagar Ganatra

Dettagli del libro
Anteprima del libro
Indice dei contenuti
Citazioni

Informazioni sul libro

Learn how you can implement routing in a React Web/Native application using React-Router library

Key Features

  • Create nested routes and complex workflows for your applications
  • Learn Routing in server-side rendered applications and in Native mobile applications
  • Understand how Redux bindings for React-Router works using the connected-react-router library

Book Description

React Router is the routing library for React, and it can be used in both React Web and React Native applications. This book is a simple way to get started with React Router and harness its full power for your applications.

The book starts with an introduction to React Router and teaches you how to create your first route using the React component. You will then learn about configuring your routes, passing parameters, and creating nested routes. You will be introduced to various components in React-Router and learn different configuration options available for these components. You will then see how to use the Redirect and Switch components. For even greater flexibility, you will learn about BrowserRouter, HashRouter, NativeRouter, and StaticRouter.

By the end of the book, you will have set up a project with React Router and make routing configuration work in a server-side rendered React application, a mobile application built with React Native and also understand how Redux and React-Router can be used in the same application.

What you will learn

  • Create your first Route using the Route component
  • Protect routes from unauthorized access by using the redirect component
  • Navigate to your defined route using Link and NavLink
  • Configure BrowserRouter and HashRouter using various props
  • Use StaticRouter for routing in server-side rendered React applications
  • Implement routing in a React Native app using react-router-native
  • Using connected-react-router library with React-Router for better state management

Who this book is for

This book is for JavaScript developers who have basic knowledge of React and who want to harness the power and flexibility of React Router

Domande frequenti

Come faccio ad annullare l'abbonamento?
È semplicissimo: basta accedere alla sezione Account nelle Impostazioni e cliccare su "Annulla abbonamento". Dopo la cancellazione, l'abbonamento rimarrà attivo per il periodo rimanente già pagato. Per maggiori informazioni, clicca qui
È possibile scaricare libri? Se sì, come?
Al momento è possibile scaricare tramite l'app tutti i nostri libri ePub mobile-friendly. Anche la maggior parte dei nostri PDF è scaricabile e stiamo lavorando per rendere disponibile quanto prima il download di tutti gli altri file. Per maggiori informazioni, clicca qui
Che differenza c'è tra i piani?
Entrambi i piani ti danno accesso illimitato alla libreria e a tutte le funzionalità di Perlego. Le uniche differenze sono il prezzo e il periodo di abbonamento: con il piano annuale risparmierai circa il 30% rispetto a 12 rate con quello mensile.
Cos'è Perlego?
Perlego è un servizio di abbonamento a testi accademici, che ti permette di accedere a un'intera libreria online a un prezzo inferiore rispetto a quello che pagheresti per acquistare un singolo libro al mese. Con oltre 1 milione di testi suddivisi in più di 1.000 categorie, troverai sicuramente ciò che fa per te! Per maggiori informazioni, clicca qui.
Perlego supporta la sintesi vocale?
Cerca l'icona Sintesi vocale nel prossimo libro che leggerai per verificare se è possibile riprodurre l'audio. Questo strumento permette di leggere il testo a voce alta, evidenziandolo man mano che la lettura procede. Puoi aumentare o diminuire la velocità della sintesi vocale, oppure sospendere la riproduzione. Per maggiori informazioni, clicca qui.
React Router Quick Start Guide è disponibile online in formato PDF/ePub?
Sì, puoi accedere a React Router Quick Start Guide di Sagar Ganatra in formato PDF e/o ePub, così come ad altri libri molto apprezzati nelle sezioni relative a Informatique e Développement Web. Scopri oltre 1 milione di libri disponibili nel nostro catalogo.

Informazioni

Anno
2018
ISBN
9781789532838
Edizione
1
Argomento
Informatique

Using StaticRouter in a Server-Side Rendered React Application

Server-Side Rendering (SSR) is a technique of rendering client-side only single-page applications (SPAs) on the server and sending the fully rendered page as a response to the user's request. In client-side SPAs, the JavaScript bundle is included as a script tag, and, initially, no content is rendered in the page. The bundle is first downloaded, and then the DOM nodes are populated by executing the code in the bundle. There are two downsides to this—on poor connections, it might take more time to download the bundle, and the crawlers that don't execute JavaScript will not be able to see any content, thus affecting the page's SEO.
SSR solves these problems by loading HTML, CSS, and JavaScript in response to the user's request; the content is rendered on the server and the final HTML is given to the crawler. A React application can be rendered on the server using Node.js and the components available in React-Router can be used to define routes in the application.
In this chapter, we will take a look at how React-Router components can be used in a server-side rendered React application:
  • Performing SSR of a React application using Node.js and Express.js
  • Adding <StaticRouter> component and creating routes
  • Understanding the <StaticRouter> props
  • Creating Isomorphic React applications by rendering the first page on the server and then allowing the client-side code to take over the rendering of subsequent pages

Performing SSR of a React application using Node.js and Express.js

In this example, we will use Node.js and Express.js to create a server-side application that will render the React application on the server. Node.js is a cross-platform JavaScript runtime environment for servers and applications. It is built on Google's V8 JavaScript engine, and it uses an event-driven, non-blocking I/O model, which makes it efficient and lightweight. Express.js is one of the most popular routing and middleware web-framework modules used in the Node.js environment. It allows you to create middleware that helps with handling HTTP requests from clients.

Installing dependencies

Let's first create a server-side application using the npm init command:
npm init -y
This will create a file, package.json, with default values for various fields. The next step is to add dependencies:
npm install --save react react-dom react-router react-router-dom express
The preceding command will add all the necessary libraries to the dependencies list in the package.json file. Please note that we are not creating a React application using the create-react-app CLI; instead, we will add the required dependencies and write the configuration files for building the application.
To build the application, the following dev dependencies are added to the devDependencies list:
npm install --save-dev webpack webpack-cli nodemon-webpack-plugin webpack-node-externals babel-core babel-loader babel-preset-env babel-preset-react 
The preceding command will add the libraries required to build the application for the devDependencies list in the package.json file.
The next step is to write a build configuration, so that the server-side application can be built.

Webpack build configuration

This is from Webpack's documentation:
At its core, WebPack is a static module bundler for modern JavaScript applications. When webpack processes your application, it internally builds a dependency graph which maps every module your project needs and generates one or more bundles.
Webpack has become the de facto standard for creating bundles for JavaScript applications. The create-react-app CLI includes scripts that internal...

Indice dei contenuti

  1. Title Page
  2. Copyright and Credits
  3. Packt Upsell
  4. Contributors
  5. Preface
  6. Introduction to React Router 4 and Creating Your First Route
  7. Configuring Routes - Using Various Options in the Route Component
  8. Using the Link and NavLink Components to Navigate to a Route
  9. Using the Redirect and Switch Components
  10. Understanding the Core Router, and Configuring the BrowserRouter and HashRouter components
  11. Using StaticRouter in a Server-Side Rendered React Application
  12. Using NativeRouter in a React Native Application
  13. Redux Bindings with connected-react-router
  14. Other Books You May Enjoy
Stili delle citazioni per React Router Quick Start Guide

APA 6 Citation

Ganatra, S. (2018). React Router Quick Start Guide (1st ed.). Packt Publishing. Retrieved from https://www.perlego.com/book/825748/react-router-quick-start-guide-routing-in-react-applications-made-easy-pdf (Original work published 2018)

Chicago Citation

Ganatra, Sagar. (2018) 2018. React Router Quick Start Guide. 1st ed. Packt Publishing. https://www.perlego.com/book/825748/react-router-quick-start-guide-routing-in-react-applications-made-easy-pdf.

Harvard Citation

Ganatra, S. (2018) React Router Quick Start Guide. 1st edn. Packt Publishing. Available at: https://www.perlego.com/book/825748/react-router-quick-start-guide-routing-in-react-applications-made-easy-pdf (Accessed: 14 October 2022).

MLA 7 Citation

Ganatra, Sagar. React Router Quick Start Guide. 1st ed. Packt Publishing, 2018. Web. 14 Oct. 2022.