To get the current route in a React TypeScript project that uses react-router-dom, you can use the useLocation hook that comes with react-router-dom. Here's an example:
index.ts338 chars14 linesIn the above example, we import the useLocation hook from react-router-dom. We then use the hook to get the current location, which includes the pathname of the current route. We initialize a state variable called currentRoute with an empty string.
We then use the useEffect hook to set the currentRoute whenever the location object changes. This ensures that currentRoute always reflects the current route.
Finally, we render the currentRoute variable inside a div element, so that we can see the current route in the UI.
gistlibby LogSnag