

Client Side Routing in React
source link: https://dev.to/leepzig/client-side-routing-in-react-24nf
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

When approaching routing in React it can be a bit daunting at first for the causal beginner, I hope that this post makes it easier to understand by breaking it up into easily digestible chunks.
Installing and Setup
We are going to be using react-router-dom
to handle our routing, if you want to look at the documentation, you can refer to it here
First we need to install react-router-dom
so run the following command in your terminal:
npm install react-router-dom
//or
yarn add react-router-dom
BrowserRouter, Switch, and Route
After you have it installed, we're going to go our highest parent component, typically that will be App.js and we are going to import BrowserRouter, Switch, and Route.
import {BrowserRouter as Router, Route, Switch} from "react-router-dom"
Then we are going to put in the basic structure for our routes. We will have BrowserRouter renamed as Router as the most outer wrapper:
import {BrowserRouter as Router, Route, Switch} from "react-router-dom"
function App() {
return (
<Router>
</Router>
)
}
Inside Router we will place our Routes as such:
import {BrowserRouter as Router, Route, Switch} from "react-router-dom"
function App() {
return (
<Router>
<Route path="/">
<Home/>
</Route>
<Route path="/about">
<About/>
</Route>
</Router>
)
}
And then finally inside Router
we will wrap all of our Routes
with the <Switch>
component like this:
import {BrowserRouter as Router, Route, Switch} from "react-router-dom"
function App() {
return (
<Router>
<Switch>
<Route path="/">
<Home/>
</Route>
<Route path="/about">
<About/>
</Route>
</Switch>
</Router>
)
}
The Switch component makes it so that if your are on the "/" root address you won't see what is in the "/about" address. You can think of it like a switch statement or like tabs where you can only see one at a time. But your url matches more than one it will display the url it matches first going from top to bottom. To prevent that we can add exact
key word to only allow the exact url to display our content:
<Route exact path="/about">
<About/>
</Route>
Link and NavLink
Okay, we have our routes, now let's make a NavBar Component so that our user can access them.
import { NavLink } from 'react-router-dom'
const NavBar = () => {
return (
<div>
<NavLink to="/">Home</NavLink>
<NavLink to="/about">About</NavLink>
</div>
)
}
The only difference between <Link>
and <NavLink>
is that when ever a Navlink route is selected the Navlink will have the link active attribute applied to it. Each Nav/Link component has to have the to
attribute to direct the user to the correct route. Notice that the value of the to
corresponds to our Routes in our App component.
Beyond The Basics
That will allow you to set up some basic client side routing, but there is actually a lot more that you can do with react-router-dom
. There are some really useful hooks like useHistory
, whicht you can use to redirect your user after an event, or `useParams to allow them follow a link for more details about something on your page. Definitely check out the documentation from React Router for more info.
I hope this gave you a good start to managing routes client side.
Recommend
-
11
Recently I deployed a Single Page Application (SPA) to Vercel (formerly Zeit). I was surprised to find that my client-side routes worked, even though my deployment only had a s...
-
12
JavaScript
-
9
June 18, 2021 ...
-
18
A coherent urn routing on the client-side React application served by Express advertisements I am currently using Express to load a one page client...
-
9
Files Permalink Latest commit message Commit time
-
6
Client-Side Rendering, Server-Side Routing Full Stack Radio
-
11
Client-side Routing without the JavaScript It's been a while since I wrote a piece about a SolidJS technology innovation. It's been tw...
-
3
Next.js client-only SPA example Made this example to show how to use Next.js router for a 100% SPA (no JS server) app. You use Next.js router like normally, but don't define getStaticProps and such. Instead yo...
-
5
Modern client-side routing: the Navigation API bookmark_border...
-
3
Server-Side Routing vs. Client-Side Routing
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK