I have build a Form that users can fill out and send via a send-button (see code below, button at the very end). It works perfectly fine but when a user clicks send, the input that this user sends just dissapeared (so the user could think sending didnt work, altough it did).
My very simple solution for that was to add an href="/xxx" to the button. That href works but if its in the button, the form is not sent when the user clicks send.
Does anyone know how I can have these two functions (sending the form and href) in one button or another way to solve that?
import React, { Component } from 'react'
import * as emailjs from 'emailjs-com'
import { Form } from 'reactstrap'
import { withStyles } from '@material-ui/core/styles';
import Button from '@material-ui/core/Button';
import { FormGroup } from '@material-ui/core';
import TextField from "@material-ui/core/TextField";
import FormHelperText from "@material-ui/core/FormHelperText";
import {indigo} from "@material-ui/core/colors";
const ColorButton = withStyles((theme) => ({
root: {
color: "#fff",
backgroundColor: "#434A7E",
'&:hover': {
backgroundColor: indigo[200],
},
fontWeight: "bold",
fontFamily: '"Segoe UI Emoji"',
},
}))(Button);
const styles = {
root: {
'& .MuiTextField-root': {
margin: "10px",
width: '100ch',
},
},
container: {
display: 'flex',
flexWrap: 'wrap',
},
textField: {
marginLeft: "5%",
marginRight: "5%",
marginTop: "10px",
marginBottom: "10px",
width: "90%",
},
messageField: {
marginLeft: "5%",
marginRight: "5%",
marginTop: "10px",
marginBottom: "10px",
width: "90%",
},
heroButtons: {
marginLeft: "25%",
marginRight: "25%",
marginTop: "10px",
marginBottom: "10px",
width: "50%",
},
};
class Buchungsformular extends Component {
state = {
email: '',
subject: '',
message: '',
anfahrt: '',
abfahrt: '',
kinder: '',
erwachsene: '',
vorname: '',
nachname: '',
}
handleSubmit(e) {
e.preventDefault()
const { email, subject, message, anfahrt, abfahrt, kinder, erwachsene, vorname, nachname } = this.state
let templateParams = {
email: email,
to_name: 'Altes Waschhaus',
subject: subject,
message_html: message,
anfahrt: anfahrt,
abfahrt: abfahrt,
kinder: kinder,
erwachsene: erwachsene,
vorname: vorname,
nachname: nachname,
}
emailjs.send(
'gmail',
'template_4CvV5FV9',
templateParams,
'user_wAhmfmSiEivROjdwXA3Ls'
)
this.resetForm()
}
resetForm() {
this.setState({
email: '',
message: '',
anfahrt: '',
abfahrt: '',
kinder: '',
erwachsene: '',
vorname: '',
nachname: '',
})
}
handleChange = (param, e) => {
this.setState({ [param]: e.target.value })
}
render() {
const { classes } = this.props; //<----- grab classes here in the props
return (
<div>
<Form onSubmit={this.handleSubmit.bind(this)}>
<div>
<FormGroup controlId="formBasicName">
<TextField
required
id="outlined-required"
label="Vorname"
defaultValue=" "
variant="outlined"
name="vorname"
className={classes.textField}
value={this.state.vorname}
onChange={this.handleChange.bind(this, 'vorname')}
/>
</FormGroup>
<FormGroup controlId="formBasicName">
<TextField
required
id="outlined-required"
label="Nachname"
defaultValue=" "
variant="outlined"
name="nachname"
className={classes.textField}
value={this.state.nachname}
onChange={this.handleChange.bind(this, 'nachname')}
/>
</FormGroup>
</div>
<FormGroup controlId="formBasicEmail">
<TextField
required
id="outlined-required"
label="E-Mail"
defaultValue=" "
variant="outlined"
name="email"
className={classes.textField}
value={this.state.email}
onChange={this.handleChange.bind(this, 'email')}
/>
</FormGroup>
<div>
<FormGroup controlId="formBasicErwachsene">
<TextField
required
id="outlined-number"
label="Erwachsene"
type="number"
InputLabelProps={{
shrink: true,
}}
className={classes.textField}
variant="outlined"
value={this.state.erwachsene}
onChange={this.handleChange.bind(this, 'erwachsene')}
/>
</FormGroup>
<FormGroup controlId="formBasicKinder">
<TextField
id="outlined-number"
label="Kinder (0-14 Jahre)"
type="number"
InputLabelProps={{
shrink: true,
}}
className={classes.textField}
variant="outlined"
value={this.state.kinder}
onChange={this.handleChange.bind(this, 'kinder')}
/>
</FormGroup>
<FormGroup controlId="formBasicEmail">
<TextField
required
id="date"
label="Anreise"
type="date"
defaultValue="2020-06-01"
className={classes.textField}
InputLabelProps={{
shrink: true,
}}
variant="outlined"
value={this.state.anfahrt}
onChange={this.handleChange.bind(this, 'anfahrt')}
/>
</FormGroup>
<FormGroup controlId="formBasicEmail">
<TextField
required
id="date"
label="Abfahrt"
type="date"
defaultValue="2020-06-01"
className={classes.textField}
InputLabelProps={{
shrink: true,
}}
variant="outlined"
value={this.state.abfahrt}
onChange={this.handleChange.bind(this, 'abfahrt')}
/>
</FormGroup>
<FormGroup className={classes.textField} controlId="formBasicMessage">
<TextField
id="outlined-multiline-static"
label="Ihre Nachricht"
multiline
rows={10}
defaultValue=" "
variant="outlined"
name="message"
value={this.state.message}
onChange={this.handleChange.bind(this, 'message')}
/>
<FormHelperText>Bitte geben sie hier an ob sie sich für die Ferienwohnung "Vilm" und/oder "Altes Waschhaus" interessieren.</FormHelperText>
</FormGroup>
</div>
<ColorButton className={classes.heroButtons} type="submit" value="Send" variant="contained" color="primary" size="large" >
Send
</ColorButton>
</Form>
</div>
)
}
}
export default withStyles(styles)(Buchungsformular)
this.resetForm()calling after send(handleSubmit)which is clearing the form.gesendetand set it totrueinsidehandleSubmit. Then whenevergesendetis true you render a "Danke" text inside your form. ;-)