Sometimes, we need optional URL path values and query parameters. URLs work best for simple options, and query parameters work best if there are many values that the component can use.
Let's implement a user list component that renders a list of users. Optionally, you want to be able to sort the list in descending order. Let's make this an optional path segment in the route definition for this page:
import React from "react";
import { render } from "react-dom";
import { BrowserRouter as Router, Route } from "react-router-dom";
import UsersContainer from "./UsersContainer";
render(
<Router>
<Route path="/users/:desc?" component={UsersContainer} />
</Router>,
document.getElementById("root")
);
The : syntax marks a variable, while the ? suffix marks the variable as optional. This means that the user can provide anything they want after /users/. This also means that the component...