I have built a web app using React.js in ES6. I currently want to create a basic \"Contact Us\" page and want to send an email. I am new to React and just discovered that I
@ryan-jenkin is completely correct.
Alternatively, if you don't have / want jQuery as a dependency, you can use the native fetch api. Also, I typically set up my form so each input has a state, then use that state in the stringified blob.
Client-side (React):
handleSubmit(e){
e.preventDefault()
fetch('/contactus', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
email: this.state.email,
// then continue this with the other inputs, such as email body, etc.
})
})
.then((response) => response.json())
.then((responseJson) => {
if (responseJson.success) {
this.setState({formSent: true})
}
else this.setState({formSent: false})
})
.catch((error) => {
console.error(error);
});
}
render(){
return (
)
}