react history push redirect code example
Example 1: react router dom push
import { useHistory } from "react-router-dom";
function HomeButton() {
let history = useHistory();
function handleClick() {
history.push("/home");
}
return (
<Button type="button" onClick={handleClick}>
Go home
</Button>
);
}
Example 2: react redirect to url
import { Route, Redirect } from 'react-router'
<Route exact path="/" render={() => (
loggedIn ? (
<Redirect to="/dashboard"/>
) : (
<PublicHomePage/>
)
)}/>