Last active
May 18, 2020 17:28
-
-
Save wdchris/c64309f50add47196c3d743292ef6660 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState } from "react" | |
const formEncode = data => { | |
return Object.keys(data) | |
.map(key => encodeURIComponent(key) + "=" + encodeURIComponent(data[key])) | |
.join("&") | |
} | |
const RegistrationForm = () => { | |
const [email, setEmail] = useState("") | |
const handleSubmit = e => { | |
fetch("/", { | |
method: "POST", | |
headers: { "Content-Type": "application/x-www-form-urlencoded" }, | |
body: formEncode({ "form-name": "register", email: email }), | |
}) | |
.then(() => alert("Success!")) | |
.catch(error => alert(error)) | |
e.preventDefault() | |
} | |
return ( | |
<form | |
name="register" | |
data-netlify="true" | |
data-netlify-honeypot="sneaky-sneaky" | |
onSubmit={handleSubmit} | |
> | |
<input type="hidden" name="form-name" value="register" /> | |
<input name="sneaky-sneaky" /> | |
<input | |
type="text" | |
name="email" | |
placeholder="Enter your email" | |
onChange={e => setEmail(e.target.value)} | |
></input> | |
<button type="submit">Register</button> | |
</form> | |
) | |
} | |
export default RegistrationForm |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment