react router dom redirect programmatically code example
Example 1: react router redirect
<Route exact path="/">
{loggedIn ? <Redirect to="/dashboard" /> : <PublicHomePage />}
</Route>
Example 2: react-router-dom redirect on click
import { useHistory } from 'react-router-dom';
function app() {
let history = useHistory();
const redirect = () => {
history.push('/your-path')
}
return (
<div>
<button onClick={redirect}>Redirect</button>
</div>
)
}