Skip to content

Instantly share code, notes, and snippets.

@wdchris
Last active May 18, 2020 17:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wdchris/c64309f50add47196c3d743292ef6660 to your computer and use it in GitHub Desktop.
Save wdchris/c64309f50add47196c3d743292ef6660 to your computer and use it in GitHub Desktop.
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