Book Image

React Router Quick Start Guide

By : Sagar Ganatra
Book Image

React Router Quick Start Guide

By: Sagar Ganatra

Overview of this book

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 ?exibility, 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.
Table of Contents (10 chapters)

Route parameters

A <Route> component in React-Router can be configured to accept URL parameters that change for a given object. For example, to display user information for a given userID, the URL path could look like '/user/1' for a user with a userID of '1', and '/user/123' for a user with a userID of '123'. The last portion of the URL is dynamic; however, in each instance, the rendered component would perform the same operation for a given userID.

An example of such a use case is Twitter's profile page. The page accepts twitterID and displays the feed for the given user.

A <Route> component can be configured to accept the dynamic portion in the URL by appending an additional path in the 'to' prop, prefixed with a colon (:) as seen here:

<Route
to='/github/:githubID'
component={GitHubComponent...