
Speed up React projects with vibe coding
Topics
How do I find the current URL of a React router?
How do I get the current base URL in React?
How do I get URL parameters in React?
How do I get the entire URL in React?
Learn how to access the current URL in a React application using React Router, the location object, query parameters, and the window interface. See practical examples for handling paths, routes, and debugging with console log in modern React projects.
When building a React application, one common question is: “How do I get the current URL?”
Whether it’s showing route-based content, handling query parameters, or working with dynamic routes, accessing the current URL is key. With tools like react router dom and the useLocation hook, you can easily read the location object and make your application more responsive to user navigation.
Before writing code, it helps to understand why developers often need the current URL in React applications. The current URL can be used for:
React applications that depend on route-specific data rely on accessing the location object. With this object, you can easily read properties such as pathname, search, and hash.
The most reliable way to obtain the current URL in React is to use the React Router library. The React Router package provides a set of hooks that allow direct access to route and location information. Among them, the useLocation hook is the most popular choice.
React Router DOM exposes this hook and returns a location object that represents the current URL. This object contains key properties such as pathname, search, and hash.
import React from "react"; import { BrowserRouter as Router, Route, Routes, useLocation } from "react-router-dom"; function Home() { const location = useLocation(); console.log("Current URL:", location.pathname + location.search); return <h2>Home Page - Current Path: {location.pathname}</h2>; } export default function App() { return ( <Router> <Routes> <Route path="/" element={<Home />} /> </Routes> </Router> ); }
In this example, the function Home component uses the useLocation hook to read the current pathname and query parameters. The console log outputs the current route every time the user navigates.
This chart shows how the flow starts from React Router DOM, goes through the useLocation hook, and gives you the location object with its properties, which can then be logged or used in your component.
The location object plays a key role when working with routes. It contains properties that represent different parts of the current URL:
This current location object gives you everything you need to manage routing and navigation effectively.
Every time you navigate, the useLocation hook returns an updated location object.
A common requirement is to read URL parameters from the route path. In React Router, you can define a dynamic route and then access its parameters using the useParams hook.
Here, the route path element home could be , which allows you to access dynamic routes. The query parameters can be combined with this to add more filters.
Routing in React enables seamless navigation between different views in single-page applications without full page refreshes. It's the mechanism that maps URLs to specific components. — Check out the full post here
To work with query parameters, you can use the API along with the property. This is particularly useful for filtering data in a React app.
The query string is extracted from the property. This is how you can filter content based on the current page state.
Sometimes, you may need the full URL, including the protocol, domain, and relative path. The window interface provides direct access through the object.
Here, the property returns the full URL, while other attributes of the window object help you get the protocol and domain separately.
Looking to build React projects faster with production-ready code? Try Rocket.new and simplify your workflow today.
If you need to reuse the logic across multiple components, you can create a custom hook. This hook can wrap around and simplify URL handling.
This custom hook makes it easy to get the current route in different components without repeating code.
Throughout development, remains one of the simplest yet most effective tools for debugging routes in a React project. By logging the or the , you can quickly confirm whether navigation updates are happening as expected. This is especially useful when dealing with or testing how your is being resolved.
For example, you might log to check the current path, or output to confirm if the query string is being captured correctly. Regular use of During development, it helps you validate routing logic before moving on to more advanced debugging methods.
Now that you know how to get the current URL in React, start applying these methods in your own projects. Whether you are building navigation flows, filtering content using query parameters, or debugging routes with console log, these techniques will help you manage your application routes effectively.
Working with the current URL in a React application is straightforward once you understand how the location object works. Whether you use the uselocation hook, the window interface, or a custom hook, you can always access and control routing effectively. With React Router, handling dynamic routes, query parameters, and full URL details becomes simple.
/user/:idURLSearchParamslocation.searchlocation.searchwindow.locationhrefuseLocationconsole loglocation objectcurrent routequery parametersdynamic routelocation.pathnamelocation.searchconsole logfunction Home() {
const location = useLocation();
console.log(location);
return (
<div>
<p>Pathname: {location.pathname}</p>
<p>Search: {location.search}</p>
<p>Hash: {location.hash}</p>
</div>
);
}
import { useParams } from "react-router-dom";
function UserProfile() {
const { id } = useParams();
console.log("User ID:", id);
return <h2>User Profile - ID: {id}</h2>;
}
export default function App() {
return (
<Router>
<Routes>
<Route path="/user/:id" element={<UserProfile />} />
</Routes>
</Router>
);
}
function SearchPage() {
const location = useLocation();
const query = new URLSearchParams(location.search);
const keyword = query.get("q");
console.log("Search keyword:", keyword);
return <p>Search keyword: {keyword}</p>;
}
function Home() {
console.log("Full URL:", window.location.href);
console.log("Protocol:", window.location.protocol);
console.log("Domain:", window.location.hostname);
return <h2>Check Console for Full URL Details</h2>;
}
import { useLocation } from "react-router-dom";
function useCurrentUrl() {
const location = useLocation();
return location.pathname + location.search;
}
function Home() {
const url = useCurrentUrl();
console.log("Custom Hook Current URL:", url);
return <p>URL: {url}</p>;
}