I have a react application where I wanted to render some menus depending upon the path.
I have following URL :
http://localhost:3000/Test/marks
Now from this URL I need to get the Test string only so that I can match that string with the array and then get the associated objects with that key.
I tried using the useLocation
but it gives us the object and again I have to add a regex to get that name.
Is there any other way ?
Thanks
2
Answers
in Your component
You mention the
useLocation
hook so I’ll assume you are usingreact-router
orreact-router-dom
. You can use theuseMatch
hook to test a path pattern, and if there is a match a match object is returned. The match object has aparams
property that will have any path params available on it.Example:
Test path:
"/Test/marks"