Controlling Redirects of Particular Routes
Learn how to navigate to a new URL by overriding the current URL.
The Redirect
component
Apart from the usual Route
component to react to particular Routes, React Router also offers a Redirect
component. The Redirect component is initialized with a to
prop in which we can provide a destination URL that the component should redirect to. It allows us to declaratively
decide in JSX where to send a particular user in certain situations. Whenever a Redirect
component is equipped with only a to
prop, a redirect to the URL provided will take place.
Often, Redirect components are a great solution to the common use case of having to redirect users to a login page if they are not logged in. Logged-in users will continue to be directed to a dashboard:
Get hands-on with 1400+ tech skills courses.