URL Parameters
When we go into a website or use any webapp, as we navigate through pages and sections, we really are just modifying our url. For example, when you go to someones twitter’s profile, the url looks something like this: https://twitter.com/borjadotai
That link would render a <Profile />
component with my user’s data on it, however, if the username was someone elses, all the data would be different, yet the component would stay the same.
This is the power of URL Parameters, they allow us to essentially inverse control and let the end user decide what component to see and pass a prop into such component.
In order to accomplish something like this, we would have to write:
And then, in order for our <Profile />
component to extract that prop
aka URL Parameter, we would have to do something like this:
useParams
being the custom hook that react-router-dom
provides for us if we have set it up correctly so that we can easily destructure any params out of the object it returns. The param will have whatever name we gave it when defining the route, in this case it was handle
.
If we want to add links to let our user navigate and essentially change that url with a click, we can do so with another tool that react-router-dom
provides us, called Link
. You can see a full example here: