React Router v4 Redirecting on form submit
For someone, who uses Hooks, have a look here -> https://reactrouter.com/web/api/Hooks/usehistory Example of use:
import { useHistory } from "react-router-dom";
let history = useHistory();
const onSubmit = () => {
//code
history.push(`/${link}/${object.id}`)
}
You have to return Redirect
in render method (otherwise it will not be rendered and as a result redirection will not happen):
render() {
const redirectToReferrer = this.state.redirectToReferrer;
if (redirectToReferrer) {
return <Redirect to="/home" />
}
// ... rest of render method code
}
I am now able to redirect to the "homepage" by swapping out <Redirect to="/home" />
with this.props.history.push("/home");
using withRouter
. Not sure why the first way would not work though.