React Router Quick Start Guide
eBook - ePub

React Router Quick Start Guide

Routing in React applications made easy

Sagar Ganatra

  1. 156 Seiten
  2. English
  3. ePUB (handyfreundlich)
  4. Über iOS und Android verfügbar
eBook - ePub

React Router Quick Start Guide

Routing in React applications made easy

Sagar Ganatra

Angaben zum Buch
Buchvorschau
Inhaltsverzeichnis
Quellenangaben

Über dieses Buch

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

Häufig gestellte Fragen

Wie kann ich mein Abo kündigen?
Gehe einfach zum Kontobereich in den Einstellungen und klicke auf „Abo kündigen“ – ganz einfach. Nachdem du gekündigt hast, bleibt deine Mitgliedschaft für den verbleibenden Abozeitraum, den du bereits bezahlt hast, aktiv. Mehr Informationen hier.
(Wie) Kann ich Bücher herunterladen?
Derzeit stehen all unsere auf Mobilgeräte reagierenden ePub-Bücher zum Download über die App zur Verfügung. Die meisten unserer PDFs stehen ebenfalls zum Download bereit; wir arbeiten daran, auch die übrigen PDFs zum Download anzubieten, bei denen dies aktuell noch nicht möglich ist. Weitere Informationen hier.
Welcher Unterschied besteht bei den Preisen zwischen den Aboplänen?
Mit beiden Aboplänen erhältst du vollen Zugang zur Bibliothek und allen Funktionen von Perlego. Die einzigen Unterschiede bestehen im Preis und dem Abozeitraum: Mit dem Jahresabo sparst du auf 12 Monate gerechnet im Vergleich zum Monatsabo rund 30 %.
Was ist Perlego?
Wir sind ein Online-Abodienst für Lehrbücher, bei dem du für weniger als den Preis eines einzelnen Buches pro Monat Zugang zu einer ganzen Online-Bibliothek erhältst. Mit über 1 Million Büchern zu über 1.000 verschiedenen Themen haben wir bestimmt alles, was du brauchst! Weitere Informationen hier.
Unterstützt Perlego Text-zu-Sprache?
Achte auf das Symbol zum Vorlesen in deinem nächsten Buch, um zu sehen, ob du es dir auch anhören kannst. Bei diesem Tool wird dir Text laut vorgelesen, wobei der Text beim Vorlesen auch grafisch hervorgehoben wird. Du kannst das Vorlesen jederzeit anhalten, beschleunigen und verlangsamen. Weitere Informationen hier.
Ist React Router Quick Start Guide als Online-PDF/ePub verfügbar?
Ja, du hast Zugang zu React Router Quick Start Guide von Sagar Ganatra im PDF- und/oder ePub-Format sowie zu anderen beliebten Büchern aus Informatique & Développement Web. Aus unserem Katalog stehen dir über 1 Million Bücher zur Verfügung.

Information

Jahr
2018
ISBN
9781789532838

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

Inhaltsverzeichnis

  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
Zitierstile für 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.