As non-fungible tokens (NFTs) continue to grow in popularity, they have also become a target for hackers and scammers in the Web3 space. Millions have been lost through various attacks, making NFT…
Routing helps you to navigate one page to another page . But Before React we had to use the “href” attribute to navigate. In that case we had to load that other page from the server which was time consuming, but with React Router that is so easy and faster because we don’t have to load that page from the server. We just render a page(component) depending on that route path. Don’t worry we will explain this(route) later . In this tutorial we will learn how to use Router in React Web App. If you do not know about React, then see this first
If you want to get started with React Routing you need a React Web App. Let’s start have a React App using yarn or npm command like this
To work with react router in web app you need to install react-router-dom module using npm or yarn command
Let’s create some demo components under “src/components” folder like this
Let’s now make route for those components in “App.js” and import some properties from “react-router-dom”
Where Router is defined for all the Route we need and Route is needed for all pages(components) we need to render . It will be clear if you see full App.js
Route has path property which determines the where we want to go if this Route is hit and component determines which page(component) will be shown if we hit that Route.
And switch renders the first path match it finds.
But there is a problem if we hit “localhost/about”
It will be like this
It should be About only for that we have to use a keyword “exact” like this
Before use of exact to React “/” and “/about” was same cause both have “/” in path string
Now “/” means to React is only and only “/”
Now hitt “/About” and it will show like this
What we expected before. It is all about basic Routing in React .In future tutorials we will discuss how we handle dynamic route and access parameters from route path.
So that’s it . Happy Coding…….
Following the approval of regulatory framework for cryptocurrencies by the South Korean government, one of the largest banks in the country is preparing to launch a range of crypto services. KB…
Attention! Confession time! As a newbie writer, I use three writing tools to help me with my stories. So, I enlisted the help of three popular writing tools to give me the reassurance that I…
We experience psychosexual bonds with two kinds of people: relatives and soulmates. Our psychosexual bonds with our relatives are involuntary and our psychosexual bonds with our soulmates are…