score:1
You've got the correct hook for getting the path
that was used when matching/rendering the route.
The
useRouteMatch
hook attempts to match the current URL in the same way that a<Route>
would. It’s mostly useful for getting access to the match data without actually rendering a<Route>
.
A match object contains information about how a
<Route path>
matched the URL.match
objects contain the following properties:
- params - (object) Key/value pairs parsed from the URL corresponding to the dynamic segments of the path
- isExact - (boolean) true if the entire URL was matched (no trailing characters)
- path - (string) The path pattern used to match. Useful for building nested
<Route>
s- url - (string) The matched portion of the URL. Useful for building nested
<Link>
s
The useRouteMatch
hook returns a match
object with the path
property that is the path used for matching.
<Route path="/base/user/:id" component={Test} />
...
function Test() {
const { path } = useRouteMatch();
useEffect(() => {
console.log(path); // "/base/user/:id"
}, [path]);
return null;
}
Source: stackoverflow.com
Related Query
- How to get the React router path on route change
- React: How to get current Route path on each route change? React-router v5
- How to get React Router to recognize a new route on a dynamic path
- How can I get the correct current path in React using react router 4.2.2's withRouter?
- React router how to get current path and look for its updates
- React router v6 how to get current route in class component
- How do you change the current path name in real time based on textbox value? react router
- React Router v4 - How to get current route?
- react router get full current path name
- React Router V4 Get Current Path
- How to scroll to top on route change with react router dom v6?
- How to get the current url path using hook router in react?
- React : How to stop re-rendering component or making an API calls on router change for the same route
- react router - get current route
- How can I match if the current route matches a pattern in React Router Dom v6?
- React How to get Component to detect route change
- How to get the current pathname correctly in React Router v6?
- How to get previous path in react router 4
- How to get the current route from react without props
- How can I get a component to update on path change while using the same route and component?
- React router v6: how to get relative index path in component?
- How to use path in react Router Route for the following format
- How to get the last part of the current URL with react router v6?
- How can i remove a Property from a react component on router path change
- How to create a dynamic subheader based on route change or params in React Router v6?
- how to get route param values of child component from parent component in app using React Router
- How to get URL parameter from a React Router path
- React router get current route outside component
- React Router how to change class name of an element in a component when route changes?
- Get Route Path by Name React Router
More Query from same tag
- replace an object inside an array with reducer
- How to map over an array that is fetched using axios
- Execute a http request before page reload
- react keeping state of render props wrapped components
- A way to shorten this code
- UseState shows previous value always
- Migration react-router v5 to v6 dynamic tab navigation
- How to download the video recorded with getDisplayMedia api?
- How to properly update an array inside a react hook state
- Using JQuery inside React
- createContext doesn't accept defaultValue
- Timers within react redux: Actions to track tick, or just start/stop
- useEffect infinitely loops when I include a dependency
- React "cannot access 'variable' before initialization" with useState callback
- Django rest framework how to pass authorizing token in nextjs post request?
- Momentum for wheel event is cancelled when child element is removed
- DatePicker form material-ui-redux-form + Redux Form - enable text input
- How to render between two elements in React?
- Mocking a React component function that is enclosed by Redux Provider component
- How can I render a ColumnChart slider
- Reading URL Params NextJS
- passing value to parent component undefined
- Can I do an Array.join() with an HTML tag in React, using JSX?
- Can't access the index of my array in async-await
- React reach router link tag updates url but not the component
- how to output current rendered component in react testing library unit test
- Mother component controlling input for child components, but doesn't rerender on change
- Rendering / Updating list stored in local storage
- Why is React.js removing the srcset tag on <img />?
- HTML5 Canvas draw lines in a circle and move with music?