// React Router v6
import { useNavigate, Redirect,Route } from 'react-router-dom';
#method 1
export function GoHome() {
let navigate = useNavigate();
const handleClick = e => {
e.preventDefault();
navigate('/home');
}
return <button onClick={handleClick}>Go to Home</button>
}
#method 2
<Route exact path="/">
{loggedIn ? <Redirect to="/dashboard" /> : <PublicHomePage />}
</Route>
<Route exact path="/">
{loggedIn ? <Redirect to="/dashboard" /> : <PublicHomePage />}
</Route>
<Route exact path="/">
{loggedIn ? <Redirect to="/profile" /> : <HomePage />}
</Route>
<Redirect to="/somewhere/else" />
state = { redirect: null };
render() {
if (this.state.redirect) {
return <Redirect to={this.state.redirect} />
}
return(
// Your Code goes here
)
}
// update the redirect
this.setState({ redirect: "/someRoute" });
state = { redirect: null };
render() {
if (this.state.redirect) {
return <Redirect to={this.state.redirect} />
}
return(
// Your Code goes here
)
}