You have to stick to the route to reach your destination. In this article we will explore about the react-router-dom package to create routing in React.
Let us continue our journey with React and understand how to make routes. This one will be straight forward and simple as we have to create bunch of routable components. The complexity arrives when we do authorized routes, but that we will keep for future. Routing is not something comes from within React, for routing we will be needing a third-party package, out of many the most suitable is React Router Dom and the NPM link can be found here.
We will head in this direction:
For our Notesy app we will have 2 pages, one is Home and another one will be About page. Along with this will have one not found page in case user enters some invalid page name direct on browser's URL. But before these we first go with the installation.
Without further ado install the react-router-dom package by running below command in the terminal.
Create a folder named "pages" and inside that add below files:
Make them appear as a normal component:
First of all take backup of the code you have in App.jsx file, because we will be again making App.jsx blank with only header and footer left. Make your App.jsx looks like this:
Add below imports:
Notice, we have given alias to BrowserRouter, this is generally we do just to avoid big names.
Now, wrap every thing within Router that is present in App.jsx:
Configure the routes as shown below:
Here, individual route is added using Route component that is wrapped within the Routes component. We are using 2 attributes of Route component namely path and element. For not found pages we use wildcard path "/*".
We can now test our routes by typing the exact path in the URL area of the browser. For now, we will not be able to see much on the pages as we have nothing added to these pages; but at least we will be able to view pages.
Add content to these pages, Home, About and NotFound:
Create a Navbar component to navigate between links:
For any SPA, we can't use anchor tags like this, otherwise your pages will reload on these links click. A solution for this is to use Link component of react-router-dom.
Link component is all fine, but when you navigate from one page to another you will not be able to see the difference as in which one is active. One hurdle with Link is that you can't have CSS classes. For customizing your links you have to use NavLink componet. Replace your Link component with NavLink.
Now, add "isActiveLink" method for updating the active class to something else, by default the name of the active class is "active":
Here, the "isActiveLink" method receives "isActive" property that let you know about the link state, if active or inactive.
Apart from Link and NavLink if we want to navigate through code, based on condition then we have to use a hook provided by react-router-dom. For this will add some UI to Footer to navigate to About page. This time we will not use Link and NavLink, instead we will use the hook.
Here, we are using a hook named useNavigate, this hook gives you a method that takes one route. We are passing the "/about" path on button click.
December 31, 2020
October 19, 2020
March 02, 2022