This section describes a Forge preview feature. Preview features are deemed stable; however, they remain under active development and may be subject to shorter deprecation windows. Preview features are suitable for early adopters in production environments.
We release preview features so partners and developers can study, test, and integrate them prior to General Availability (GA). For more information, see Forge release phases: EAP, Preview, and GA.
This hook returns the current location object, which contains information about the current URL. It updates automatically when the user navigates to a different route.
It must be used within a Router component.
To add the useLocation hook to your app:
1 2import { useLocation } from '@forge/react/router';
Here is an example of an app that displays the current path using useLocation.
1 2import ForgeReconciler, { Text, Code } from '@forge/react'; import { Router, Route, useLocation } from '@forge/react/router'; const CurrentPath = () => { const location = useLocation(); return ( <Text> Current path: <Code>{location.pathname}</Code> </Text> ); }; const App = () => ( <Router> <Route path="*"> <CurrentPath /> </Route> </Router> ); ForgeReconciler.render(<App />);
1 2import type { Location } from 'history'; function useLocation(): Location; interface Location { pathname: string; search: string; hash: string; key: string; }
None.
string): The current URL path (e.g. /settings).string): The query string portion of the URL, including the leading ?
(e.g. ?filter=active). Empty string if there is no query string.string): The hash portion of the URL, including the leading # (e.g. #section).
Empty string if there is no hash.string): A unique key identifying this location.Rate this page: