React Router Special Cases
Let's look at a few special cases where we'll be using advanced routing techniques.
Passing URL Parameters between Routes
URL parameters are variables that we define in the routes and which can take a special form. We will use those parameters to handle cases where we need to pass information into the route via the URL and to create SEO-friendly links such as the following:
/recipes/stews/meat
Another example of this is as follows:
/users/theo/profile
To define URL variables, we need to prepend them with a colon (:
), followed by the name of the parameter. For example, the following strings are examples of valid parameters:
:id, :username, :email, :type, :search
However, the following strings are not valid parameters:
?id, -username, &email, type, *search*
When we use parameters like this, we just need a valid link to match that route. We can access the matched parameter by name using the useParams()
function...