Mark As Completed Discussion

In React Router, redirects are used to navigate users to specific routes. This is useful when you want to automatically redirect users to a different route based on certain conditions or events.

To implement redirects in React Router, you can use the Redirect component or programmatically navigate using the history object.

The Redirect component allows you to define a specific path and URL you want to redirect users to. For example, to redirect users from the '/login' route to the '/dashboard' route, you can use the following code:

JAVASCRIPT
1import { Redirect } from 'react-router-dom';
2
3function LoginPage() {
4  const isLoggedIn = true; // Replace with your authentication logic
5
6  return (
7    <div>
8      {isLoggedIn ? <Redirect to='/dashboard' /> : <LoginForm />} // If the user is logged in, redirect to '/dashboard'
9    </div>
10  );
11}
JAVASCRIPT
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment