In this article, we will learn about Query params in React Router and also we see Search params in React Router. Below you can see complete details about Query Params and Search Params in React Router with examples.
In the previous article, we learned about dynamic routes with react-router-dom. we created the user's listing page and nested the user's details page. it's all working great but there is room for improvement.
At the moment we just display the text details about the user, irrespective of who the user is.
In a typical application, you would want to extract the userId and do something
with that id, perhaps make an API call to fetch the details about that user.
React Router V6 Query Params
For our scenario let's keep it simple and display the user id from the URL in the component.
Now, to extract the route parameter we need to import a hook from the react-router package.
In the user details file at the top import useParams from react-router-dom
import { useParams } from 'react-router-dom'
And then call the hook within the component use params with parentheses, the hook returns an object of key-value pairs. Let's call it params, so const params are equal to use params, this object contains key-value pairs of the dynamic params from the current URL, the parameter we want to access is user Id.
const params = useParams()
Let's store that in a constant, const user id is equal to params.userId
const userId = params.userId
It is important to note here that userId on the params object, corresponds to the dynamic segment we
have specified in the route config (App.js), colon user Id.
App.js
import { Component } from 'react'import { Routes, Route } from 'react-router-dom'import { UserDetails } from './components/UserDetails';import { Users } from './components/Users';function App() {return (<Routes><Route path="users" element={<Users />} ><Route path=":userId" element={<UserDetails />} /><Route path="admin" element={<Admin />} /></Route><Route path="*" element={<PageNotFound />} /></Routes>);}export default App;
Once we have the user id, we can render it as part of JSX details about the user and we render the user Id.
return (<div>Details about user {userId}</div>)
Here are the complete UserDetails.js file
UserDetails.js
import React from 'react'import { useParams } from 'react-router-dom'export const UserDetails = () => {const params = useParams()const userId = params.userIdreturn (<div>Details about user {userId}</div>)}
If we now save the file and head back to the browser, navigate to slash users slash one and we see the text details about user one
http://localhost:3000/users/1
Change the Id to slash two, we see details about user two
http://localhost:3000/users/2
Change the Id to slash 100, details about user 100.
So this is pretty much how you get hold of URL params in your component, import the use params hook and invoke it and access the property on the returned object.
React Router V6 Search Params
<button>Active Users</button><button>Reset Filter</button>
import { Outlet, useSearchParams } from 'react-router-dom'
const [searchParams, setSearchParams] = useSearchParams()
<button onClick={() => setSearchParams({ filter: 'active' })}>Active Users</button>
<button onClick={() => setSearchParams({})} >Reset Filter</button>
const showActiveUsers = searchParams.get('filter') === 'active'
{showActiveUsers ? (<h2>Showing active users</h2>) : (<h2>Showing all users</h2>)}
import React from 'react'import { Outlet, useSearchParams } from 'react-router-dom'export const Users = () => {const [searchParams, setSearchParams] = useSearchParams()const showActiveUsers = searchParams.get('filter') === 'active'return (<div><h2> User 1</h2><h2> User 2</h2><h2> User 3</h2><Outlet /><div><button onClick={() => setSearchParams({ filter: 'active' })}>Active Users</button><button onClick={() => setSearchParams({})} >Reset Filter</button></div>{showActiveUsers ? (<h2>Showing active users</h2>) : (<h2>Showing all users</h2>)}</div>)}
0 Comments