Redirecting
Thus far, all the explored navigation options (links and programmatic navigation) forward a user to a specific page.
In most cases, that's the intended behavior. But in some cases, the goal is to redirect a user instead of forwarding them.
The difference is subtle but important. When a user is forwarded, they can use the browser's navigation buttons (Back
and Forward
) to go back to the previous page or forward to the page they came from. For redirects, that's not possible. Whenever a user is redirected to a specific page (rather than forwarded), they can't use the Back
button to return to the previous page.
Redirecting users can, for example, be useful for ensuring that users can't go back to a login page after authenticating successfully.
When using React Router, the default behavior is to forward users. But you can easily switch to redirecting by adding the special replace
prop to the Link
(or NavLink
) components, as follows: