useParams
access parameters in the URL.
// <Route path="/projects/:projectId" component={ProjectDetails} /> import { useParams } from 'react-router-dom'; const ProjectDetails = () => { const { projectId } = useParams(); return <div>Project ID: {projectId}</div>; };
useLocation
extracts location object which provides information about the current URL.
import { useLocation } from 'react-router-dom'; const CurrentPath = () => { const location = useLocation(); return <div>Current Path: {location.pathname}</div>; };
useSearchParams
specifically created to work with query parameters in the URL.
import { useSearchParams } from 'react-router-dom'; const QueryParamsExample = () => { const searchParams = useSearchParams(); const queryParamValue = searchParams.get('paramName'); return <div>Query Parameter Value: {queryParamValue}</div>; };
useParams
useParams
returns an object of key/value pairs of URL parameters.
Use it to access match.params
of the current <Route>
.
import React from "react"; import { BrowserRouter as Router, Switch, Route, useParams } from "react-router-dom"; function BlogPost() { let { slug } = useParams(); return ( <div>Now showing post: {slug}</div> ); } ReactDOM.render( <Router> <Switch> <Route exact path="/"> <HomePage /> </Route> <Route path="/blog/:slug"> <BlogPost /> </Route> </Switch> </Router>, node );
useSearchParams
useSearchParams
returns a URLSearchParams
object, which can be used to read and write to the query string of the current URL
const location = useLocation(); const params = useSearchParams(location.search); // Get a single value const name = params.get("name"); // Set a single value params.set("name", "John"); // Get all values const allNames = params.getAll("name"); // Delete a single value params.delete("name");
You HAVE to use .get()
or .set()
to read or write to the query string.
If you don't use the .get()
method, you will receive an object instead of a string value like {size:2}
.
const location = useLocation(); const params = useSearchParams(location.search); // what you want const val1 = params.get("field_name"); // "value" // not what you want const val2 = params; // {size: 1}
state
back to the URL as a query stringUseful for pagination default values.
// use React Router history/location const history = useHistory(); const location = useLocation(); // using window const history = window.history; const location = window.location; useEffect(() => { // get the current URL query string const params = new URLSearchParams(location.search); // get the value of the updated field_name query string const value = params.get("field_name"); // if the value is not found if(!value) { // set the state to default value setNewValue(defaultValue); // set the default value to the query string params.set("field_name", defaultValue.field_name); // set another value to the query string params.set("field_name_2", defaultValue.field_name_2); } }, [window.location.search]) const newSearchParams = new URLSearchParams(window.location.search); // sets new Url query string newSearchParams.set("field_name", newValue.toString()); // set new value to the state setNewValue(newValue); // pushes new Url query string using React router history history.push( { pathname: window.location.pathname, search: newSearchParams.toString() } )