Skip to content

Instantly share code, notes, and snippets.

@elitan
Last active November 9, 2022 05:21
Show Gist options
  • Save elitan/5e4cab413dc201e0598ee05287ee4338 to your computer and use it in GitHub Desktop.
Save elitan/5e4cab413dc201e0598ee05287ee4338 to your computer and use it in GitHub Desktop.
React Router V4 Redirect after form submission
import React, { Component } from 'react';
import { withRouter } from 'react-router-dom'; // <--- import `withRouter`. We will use this in the bottom of our file.
class ContactForm extends Component {
submitForm (e) {
e.preventDefault()
this.props.history.push('/thank-you'); // <--- The page you want to redirect your user to.
}
render() {
return (
<div>
<form onSubmit={this.submitForm.bind(this)}>
<button type="submit">Submit</button>
</form>
</div>
);
}
}
export default withRouter(ContactForm); // <--- make sure to wrap your component with `withRouter()`
@nicolassso
Copy link

Thanks a lot!

@luizhcordeiro
Copy link

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment