question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to redirect the page using withRouter HOC after handleSubmit is successful?

See original GitHub issue

Can 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:closed
  • Created 6 years ago
  • Comments:13 (1 by maintainers)

github_iconTop GitHub Comments

16reactions
prichodkocommented, Dec 11, 2017

Second argument in handleSubmit includes props. You should be able to get history from there.

7reactions
jay-jlmcommented, Jul 21, 2018

This is what worked for me:

<WrappedFormComp {...props} />

Now that you passed history via prop drilling, you can write:

const formEnhancer = withFormik({
   [...]
    handleSubmit(values, { props }) {
      props.history.push('/some/funky/path')
    }
  })
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found