The best way to redirect a page in React.js depends on what is your use case. Let us get a bit in detail:

If you want to protect your route from unauthorized access, use the component and try this:

Example

                                        

import React from 'react'
import { Redirect } from 'react-router-dom'
const ProtectedComponent = () => {
   if (authFails)
   return <Redirect to='/login' />
}
return <div> My Protected Component </div>
}

Suggest An Answer

No suggestions Available!