How to redirect the page using withRouter HOC after handleSubmit is successful?
See original GitHub issueCan I have an example/snippet which is using react-router-v4 to redirect after successful submission? I am not sure how to use withRouter HOC with withFormik HOC?
If I could get props.history inside handleSubmit, then i can redirect the user to new page using props.history.push(‘/any-new-page’)
const EnhancedForm = withFormik({
mapPropsToValues: () => ({
surveyTitle: '',
surveyCategory: ''
}),
validationSchema: Yup.object().shape({
surveyTitle: Yup.string().required('Survey name is required'),
surveyCategory: Yup.string().required('Survey category is required')
}),
handleSubmit: (values, { setSubmitting }, history) => {
setTimeout(() => {
alert(JSON.stringify(values, null, 2));
setSubmitting(false);
// How to get history props from withRouter HOC to here
history.push('/any-new-page');
}, 1000);
},
})(MyInnerForm);
Issue Analytics
- State:
- Created 6 years ago
- Comments:13 (1 by maintainers)
Top Results From Across the Web
ReactJS redirecting to another page on successful statement ...
You can use the withRouter HOC to provide the history api to your props. import { withRouter } from "react-router"; handleSubmit() { let ......
Read more >Redirect on form submit using React Router | bobbyhadz
To redirect on form submit using React Router, use the `useNavigate()` hook, ... const navigate = useNavigate(); const handleSubmit = event => {...
Read more >How can I redirect after successful post? [React/Redux/Node ...
You can either pass down props from the Route, or write a method in the Route that does the redirect and pass that...
Read more >Redirect with React Router - Medium
In this example, I am using the Redux pattern so I pass handleSuccess to the addPost action and call it if the POST...
Read more >How to Router Redirect After Login - Pluralsight
HOC for Authentication. To authenticate the user across pages, we need to create a higher-order component (HOC) to wrap the < ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Second argument in
handleSubmit
includesprops
. You should be able to get history from there.This is what worked for me:
Now that you passed history via prop drilling, you can write: