import React, { Component } from 'react'; const encode = (data) => { return Object.keys(data) .map(key => encodeURIComponent(key) + "=" + encodeURIComponent(data[key])) .join("&"); } class ContactForm extends Component { constructor(props) { super(props); this.state = { name: "", email: "", message: "" }; } handleSubmit = e => { fetch("/", { method: "POST", headers: { "Content-Type": "application/x-www-form-urlencoded" }, body: encode({ "form-name": "contact", ...this.state }) }) .then(() => alert("Success!")) .catch(error => alert(error)); e.preventDefault(); }; handleChange = e => this.setState({ [e.target.name]: e.target.value }); render() { const { name, email, message } = this.state; return (
); } } export default ContactForm;