How To Use React Router in Your Application
How To Use React Router in Your Application
What is Routing?
The process of moving between different views of an application when a user
clicks an element (link, button, image etc.) or enter a URL within the application is
referred to as routing in React.
Routing is the process of keeping the browser URL in sync with what’s being rendered
on the page. i.e if a user should navigate to href=’/about’ then the about page component
should be rendered accordingly.
Let’s quickly look at how we can setup a React-router in our React application.
Installation
npm install react-router-dom
Three (3) basic components in React-Router
I. React routers: React router API’s such as <BrowserRouter> and <HashRouter>
- <BrowserRouter>: which is the most used because it uses regular URL paths. e.g.
http://www.oci.com.ng/about
- <HashRouter>: which stores the current location in the hash portion of the URL. e.g.
http://www.oci.com.ng/#/about.
To use a router, just make sure it is rendered at the root of your element hierarchy.
Which means you have to wrap your top-level component (<App />) in a router.
When a user clicks or navigate to a location (URL) that correspond to path=”/about”, the
About component gets rendered on the UI.
Switch
A switch component works just like our normal Switch statement. A <Switch> checks
through all routes wrapped inside and rendered the exact Route path that matches the
current URL then stop (i.e after matching a particular path and rendering the UI, it
doesn't check the rest Routes).
switch diagram illustrating the above explanation
<Link >
The link component works exactly like the anchor element (<a
href=”/about”>About</a>) in HTML. The advantage of using the Link component is
that it eliminates browser refresh as propose to anchor tag. It is used to navigate between
pages, automatically rendering a new view (UI) each time.
<Link to=”/about” >About </Link>
<NavLink>
The NavLink renders the same function as <Link> except for the extra features added.
The NavLink comes with properties such as
(i) activeClassName
(ii) activeStyle
(iii) isActive
(iv) strict
activeClassName
Refers to the class to give the element when it is active. The default class given is
active.
Let’s create a class called active in our stylesheet and import it to our App components.
.active{color:red}
activeStyle(object)
It refers to the style we want to apply to the element when it is active.
Const active= {{fontSize: ‘2rem’, color: ‘red’, textDecoration: ‘underline’}}
We can then apply the active object(active) which is a css style to our component.
isActive (function)
Is a function used to add extra logic for determining if a link is active or not.
<Redirect>
Have you ever tried redirecting a user to a different location when a particular condition
is fulfilled or after a form is been submitted and validated? The Redirect component acts
just like the server-side redirect do.
Conclusion
In this first part, we were able to establish the concept of react-router, its
importance’s, and how to setup react router in your application with the basic component
needed to get you started.
In out next series, we are going to look at how we can use all this to create a budgetary
application
With this knowledge, you are absolutely ready to start writing complex react
applications.
I hope you found this article useful? If you did kindly follow me and hit the LIKE button
to get updates on my upcoming article on protected routes and other topics lined up for
you.
Thanks for staying around and hope to see you on my next article.
About
Written by
Joshua Oyewole