Last active
May 27, 2021 19:32
-
-
Save Addono/68f16399c33c6f9686b72a1557aacd3a to your computer and use it in GitHub Desktop.
NextJS HubSpot Email Collection Form
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 { useEffect, useState } from "react" | |
import useAxios from "axios-hooks" | |
const EmailSignupForm = () => { | |
const [email, setEmail] = useState("") | |
const [pageUri, setPageUri] = useState<string>() | |
const [{ data, loading, error }, refetch] = useAxios( | |
{ | |
url: "/api/emailSignup", | |
method: "POST", | |
data: { email, pageUri }, | |
}, | |
{ | |
manual: true, | |
} | |
) | |
// Clear the form on successfull submitting it | |
useEffect(() => { | |
if (data?.success === true && !loading) { | |
setEmail("") | |
} | |
}, [data?.success, loading]) | |
// Get the url the user is currently visiting. | |
// Optional, but enriches the data we have in HubSpot. | |
useEffect(() => { | |
setPageUri(window.location.href) | |
}) | |
return ( | |
<> | |
<input | |
type={"email"} | |
placeholder={"mail@example.com"} | |
value={email} | |
onChange={(e) => setEmail(e.target.value)} | |
/> | |
<button | |
type={"submit"} | |
onClick={() => refetch()} | |
disabled={loading} | |
> | |
Signup | |
</button> | |
</> | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment