Skip to content

Instantly share code, notes, and snippets.

@joshuachinemezu
Forked from verticalgrain/app.js
Created February 15, 2018 01:05
Show Gist options
  • Save joshuachinemezu/3eb30f395790f21c1c39426478ce009a to your computer and use it in GitHub Desktop.
Save joshuachinemezu/3eb30f395790f21c1c39426478ce009a to your computer and use it in GitHub Desktop.
React Router V4 Redirect after form submission
import React, { Component } from 'react'
import { Redirect } from 'react-router'
export default class ContactForm extends Component {
constructor () {
super();
this.state = {
fireRedirect: false
}
}
submitForm = (e) => {
e.preventDefault()
this.setState({ fireRedirect: true })
}
render () {
const { from } = this.props.location.state || '/'
const { fireRedirect } = this.state
return (
<div>
<form onSubmit={this.submitForm}>
<button type="submit">Submit</button>
</form>
{fireRedirect && (
<Redirect to={from || '/thank-you'}/>
)}
</div>
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment