
Build 10x products in minutes by chatting with AI - beyond just a prototype.
Topics
How to use useMatch in React?
Is react-router DOM deprecated?
What replaced useHistory in react-router dom?
Using React Router requires a good understanding of routing. The useMatch hook in React Router is what determines if the current URL matches a path.
This blog will show you how to use the useMatch React Router feature for dynamic and nested routing and a smooth user experience.
Also you will learn route matches, current route, route path and how the match object helps extract params from the URL. With a practical implementation focus, this blog will show you how to use the useMatch hook in different parts of your React app.
The useMatch hook in React Router checks if the current URL aligns with a given route path and returns a match object with valuable properties. It helps developers handle dynamic routes, extract parameters, and control how components render based on the current route.
• Returns an object when the current route matches the provided route path.
• Works with nested routes to determine relative matches.
• Extracts dynamic parameters from the current URL.
• Helps in conditional rendering of components based on route matches.
• Returns null if the current URL does not match the provided path.
The useMatch hook requires a route path as an argument and returns a match object if a match is found.
import { useMatch } from "react-router-dom"; const MyComponent = () => { const match = useMatch("/user/:id"); return ( <div> {match ? <p>Matched User ID: {match.params.id}</p> : <p>No Match Found</p>} </div> ); };
When a current route matches the provided path, the match object includes:
• pathname: The matched URL.
• params: An object containing key-value pairs of parameters from the route path.
• pattern: Defines the structure of the matched path.
If there is no match, the hook returns null.
The useMatch hook replaces useRouteMatch , which was used in previous versions of React Router DOM. Here’s a comparison:
| Feature | useMatch (v6+) | useRouteMatch (v5) |
|---|---|---|
| Returns match object | ✅ Yes | ✅ Yes |
| Requires explicit route path | ✅ Yes | ❌ No (auto-detects) |
| Supports relative paths | ✅ Yes | ❌ No |
| Works with nested routes | ✅ Yes | ✅ Yes |
| Returns null for no match | ✅ Yes | ❌ No (returns false) |
The useMatch hook detects when a current URL aligns with a defined route path.
For dynamic routes, parameters can be extracted from the match object.
The useMatch hook works effectively with nested routes by determining relative matches.
The params object from the match object allows extracting values from dynamic routes.
When the current route does not match, useMatch returns null. This can be handled with conditional checks.
The useMatch hook is useful for:
• Checking if the current URL matches a specified route path.
• Extracting parameters from dynamic routes.
• Rendering components conditionally based on route matches.
• Handling nested routes in React Router DOM.
The useMatch React Router hook makes matching easier. It helps extract params, manage nested routes and check the current location without the hassle.
By understanding the match object, route path and URL you can create better navigation in your React projects. This hook simplifies the routing logic so you can handle dynamic paths in functional components.
import { useMatch } from "react-router-dom";
const HomePage = () => {
const match = useMatch("/home");
return (
<div>
{match ? <h1>Welcome to the Home Page</h1> : <h1>Not on Home Page</h1>}
</div>
);
};import { useMatch } from "react-router-dom";
const UserProfile = () => {
const match = useMatch("/user/:userId");
return (
<div>
{match ? <p>User ID: {match.params.userId}</p> : <p>No User Found</p>}
</div>
);
};import { Routes, Route, useMatch } from "react-router-dom";
const Dashboard = () => {
const match = useMatch("/dashboard");
return (
<div>
<h1>Dashboard</h1>
{match && <p>Inside Dashboard</p>}
</div>
);
};
const AppComponent = () => (
<Routes>
<Route path="/dashboard/*" element={<Dashboard />} />
</Routes>
);const ProductPage = () => {
const match = useMatch("/product/:productId");
if (match) {
return <h1>Product ID: {match.params.productId}</h1>;
}
return <h1>Product Not Found</h1>;
};const BlogPage = () => {
const match = useMatch("/blog/:id");
return (
<div>
{match ? <p>Viewing Blog ID: {match.params.id}</p> : <p>No Blog Found</p>}
</div>
);
};import { Routes, Route, useMatch } from "react-router-dom";
const Sidebar = () => {
const match = useMatch("/dashboard");
return (
<nav>
{match ? <p>Sidebar visible on Dashboard</p> : <p>Sidebar hidden</p>}
</nav>
);
};
const Dashboard = () => <h1>Dashboard Page</h1>;
const HomePage = () => <h1>Home Page</h1>;
const App = () => (
<Routes>
<Route path="/" element={<HomePage />} />
<Route path="/dashboard" element={<Dashboard />} />
<Sidebar />
</Routes>
);