Can Correct Urination Reduce the Incidence of Prostatitis?

As a consequence of irregular life, high work tension and rising age, more and more men commence getting entangled with prostatitis. Prostatitis not only has a great influence on men ‘s job and life…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




A Quick Guide to Understanding React Routers

Why react-router?

Prerequisites

To be able to fully grasp this tutorial, it is expected that you have:

Install React Router

The first step to using React Router will be installing the appropriate package. There are basically three (3) different packages: React Router, React Router DOM, and React Router Native. The React Router is the core dependency used for the other two packages listed above. React Router DOM is for building web applications while React Router Native is for mobile applications built with React Native. We install React Router DOM from our command line by writing:

using npm

using yarn

SETTING UP REACT ROUTER

After installation is complete we need to set up the router for use within our react project. We do this with a component called BrowserRouter. Browser Router keeps track of route history in the react app. To import we write:

Note that it's also a practice to add an alias as Router when importing the BrowserRouter. It can be written as:

To provide routes within our application, we import the Route component and then wrap up our application within routes:

This way we declare individual routes within our application. You cannot access any router specific data that is declared outside the router. To do that we use history data.

USING THE ROUTE COMPONENT

To use the routes we declare them with two props for every route: path and component.

We can then import this into our app and use a route:

The path props tells us what path our route is located on. The component prop contains the component for the declared path. Numerous routes can be declared within the router. Alternatively, we can also write a route without the component prop:

You can display a component in any of the routes by adding the component within the Router:

At this point, we notice that when we have more than one route within our router we see all the contents for all the routes when we visit each route component. We solve this using the Switch Component:

We then wrap our Routes within the Switch:

The Switch component is used to check the child routes and display only the one in which the URL matches the current path.

CREATING NAVIGATION

We can navigate through our routes with the help of a Link component:

We then write the Link with a prop To which contains the path to the component to be rendered:

PAGE NOT FOUND (404)

Using routes, we can also create a custom 404 page in the event that a user navigates to a path that doesn’t exist within the application. To do that we create a Route with a path of *:

Any time the user navigates to a path that does not exist they will be navigated to the NotFound component.

USE HISTORY HOOK

React Router DOM provides us with a component called use History which allows us to return information associated with our component such as the path associated with our current component.

PASSING DATA BETWEEN ROUTES

We can pass a value from one page to another page routed to within our path by adding it within our Route by adding it as a parameter:

If we wish to pass on a variable such as an item name from within the Result Component referenced above we can add it within our Link.

We can then access the information from the path on the routed page from the link by referencing it as a params:

CONCLUSION

The purpose of this blog is to give you a quick insight on how to implement React router for use within your React app. With this, you should be able to make use of the react-router DOM to build web applications.

Add a comment

Related posts:

Climate Change Offers an Opportunity to Create Millions of Jobs

Each and every sector; from manufacturing, processing, agricultural, mining and service industries depend on natural resources. Across the world, many countries have agriculturally based economies…

10 dicas para uma gravidez tranquila

Muita gente literalmente pira durante a gravidez. Mas não precisa ser assim. Seguem algumas dicas que podem facilitar a vida da mulher nesse momento tão especial. Estude. Isso mesmo. Na escola não…

Crypto vs. Capital Markets in the Time of Crisis

Capital markets and crypto have been under constant pressure given the geopolitical crisis between Russian and Ukraine. Last night, Russia launched a full invasion into the Ukrainian territory…