Learn React Router in 5 Minutes – Beginner’s Guide
Sometimes you’ve only got 5 minutes to spare. Instead of wasting it, let’s get a 5-minute introduction to React-Router! In this tutorial, we’re going to learn the basics of routing in React by building navigation for a myshop Ecommerce website.
Many modern websites are actually made up of a single page. They just look like multiple pages because they contain components which render like separate pages. These are usually referred to as SPAs — single-page applications.At its core, what React Router does is conditionally render certain components to display depending on the route being used in the URL (/ for the home page, /about for the about page, etc.).
Purpose of React Router:
React Router, and dynamic client-side routing, allow us to build a single-page web application and navigation without the page refreshing as the user navigates. React Router uses a component structure to call components based on the route, which displays the appropriate information.
Installing React Router:
You’ll need to import BrowserRouter, Route, and Switch from react-router-dom package:
First, you’ll need to set up your app to work with React Router. Everything that gets rendered will need to go inside the element, so wrap your App in those first. It’s the component that does all the logic of displaying various components that you provide it with.
Next, in your App component, add the Switch element (open and closing tags). These ensure that only one component is rendered at a time. If we don’t use this, we can default to the Error component, which we’re going to write later.
It’s now time to add your tags. These are the links between the components and should be placed inside the tags.
To tell the tags which component to load, simply add a path attribute and the name of the component you want to load with component attribute.
Many homepage URLs are the site name followed by “/”, for example, www.myshop.com/. In this case, we add exact to the Route tag. This is because the other URLs also contain “/”, so if we don’t tell the app that it needs to look for just /, it loads the first one to match the route, and we get a pretty tricky bug to deal with
Now import the components into the app. You may wish to have them in a separate “components” folder to keep code clean and readable
Onto that error message I mentioned earlier, which loads if a user types an incorrect URL. This is just like a normal tag, but with no path. The Error component contains <h1>Oops! Page not found!<h1>. Don’t forget to import it into the app
So far, our site is only navigable by typing the URLs. To add clickable links to the site, we use the Link element from React Router and set up a new Navbar component. Once again, don’t forget to import the new component into the app.
Now add a Link for each component in the app and use to=”URL” to link them
Your site now has clickable links that can navigate you around your single-page app!
So there we have it. If you want to easily navigate around a React app, forget the anchor tags and add React Router. It’s clean, it’s organized, and it makes adding and deleting pages a whole lot easier.Mildaintrainings brings you a virtual, hands-on React JS training course. Enroll & Get Certified now!