How to set up React Router
Tags: react, beginners, tutorial, webdev
Published: 2021-06-06 21:12:48

One of the most advantageous parts of using the JavaScript library, React, is the existence of the virtual DOM. The virtual DOM makes Single Page Applications so powerful with its dynamic loading.
 
Because React is only concerned with the state of its components, If set up correctly, it only re-renders the components that need to be updated. This makes it possible for dynamic rendering to happen.

If you want to switch pages in a react application there are ways to do it dynamically, such as conditional rendering of a “Page”. Conditional rendering is useful for small, non-complex applications. For more robust applications that may have a more intricate tree structure, there is a more powerful tool. 

React Router is a library for handling routing in React. It is more powerful than conditional rendering by allowing the developer to use, redirection, parameters, dynamic URL’s, and Props. 

For the sake of this tutorial, we will only go over setting up the basics for a React application to use React Router. 

To begin, You need to create a new React application of course. If You don’t know how to do this, check out my tutorial on setting up a react application [here](https://dev.to/cosmiceli/setup-a-new-react-application-1f86). 

Once you have your application all set up, We are going to set up the application to make it as basic as possible. 

We first will go into the project’s src directory and remove every file inside.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/obpqvuvcoiel12bnn6ed.JPG)

Next, we are going to make 5 simple files, App.js, index.js, Page1.js, Page2.js, and Page3.js. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jnk0bvpc3pwvrv740aqx.JPG)

After this is finished, we will set up the index.js to render the App.js function. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/awxdwml00rm7s4y8ixdu.png)
Next, we will set up the App.js component, and the Page components.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jnmkz242929gh291jes5.png)

Page1.js
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8y91m9brrmnqsgd80ykw.png)

Page2.js
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sf4xij2gkb4m6u1cfidz.png)
Page3.js
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kf151d271dmu5g3et03a.png)


Now that this step is complete, we will install the react-router-dom dependency so we can set up React Router. We just need to open the terminal in the directory where our react project is and use the command 'npm install react-router-dom'. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/er67il0olxqbmijh4t3v.png)

Once we’ve done this, we can start setting up react-router. 
In the App component, we need to import some components from react-router.
These components are Browser Router, Switch, and Route
We also need import the Page components, and wrap them in routes.

With this done, we can now set up our Router. 
The router will allow any components inside of it to be routed. 

Inside the routes we need to set the route paths that determine which component will be rendered. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ziedzvt7a98utg60uq5x.png)

With this done, We can now start the application and go to any of the routes that we set up, and the components will be rendered. 
the 3 routes are 

localhost:3000
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gq9rhmylonsgo8yt6xow.JPG)
localhost:3000/page2
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6kg4v0smwtumj2id9492.JPG)
localhost:3000/page3
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gnpamax0xzjahsr2t3cg.JPG)

We are essentially finished but we can take it a couple of steps further to really drive it home. 

We can add links to each of the pages so that we can switch pages whenever we need to. All we need for this, is to import Link from react router. 

page 1
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ps49hn79xrbbgoevwgtv.png)

page 2
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u8srgvqbyyhwjm3cf9rm.png)

page 3
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/giuyhymamwpxrcyyfwm4.png)

Now we have a directory on every page
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5srn0a7o9xnc69va4ar3.JPG)

Finally, If we wanted to have a navigation bar at the top of the page for every page, we don't need to add it to every page component, we only need to add it at the top of the router in the app component. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/67637vrnry8dbynhhr2g.png)


Now We have a 'navbar' on every page!
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cmzcly2iw8j2la6b5s3k.JPG)


This is all we need to get started with using react router. Now you should be able to setup simple routes, and links. You can build on this knowledge to create redirections, histories, directories, navbars, and much more.