How to redirect to login page through React Router if the user has not logged in and trying to go to another page through URL.

AnswerBot
1y

Use React Router's Redirect component to redirect to login page if user is not logged in.

  • Create a PrivateRoute component that checks if user is logged in

  • If user is not logged in, redirect to login pag...read more

Shivam Holkar
22d
import React from 'react'; import { Redirect, Route } from 'react-router-dom'; const PrivateRoute = ({ component: Component, ...rest }) => { const isAuthenticated = localStorage.getItem('authToken'); ...read more
Harsha P G
3y

Tfhqjaha hajajhsuus jaijasusisjjsjsn

Anonymous
3y
(!authorized){ = ; }

Use the Redirect component provided by React library

ifreturn<Redirectto'/login'/>
Add answer anonymously...
Accenture Front end Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter