Last active
April 5, 2021 19:30
-
-
Save thetrevorharmon/6f308dc9f97aaa050f6e1424dce0890d to your computer and use it in GitHub Desktop.
Gatsby Mailchimp Signup 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 addToMailchimp from 'gatsby-plugin-mailchimp'; | |
import React, { useState } from 'react'; | |
import * as styles from './EmailListForm.module.scss'; | |
const EmailListForm: React.FunctionComponent<{}> = () => { | |
const [email, setEmail] = useState(''); | |
const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => { | |
e.preventDefault(); | |
addToMailchimp(email) | |
.then((data) => { | |
alert(data.result); | |
}) | |
.catch((error: Error) => { | |
// Errors in here are client side | |
// Mailchimp always returns a 200 | |
}); | |
}; | |
const handleEmailChange = (event: React.ChangeEvent<HTMLInputElement>) => { | |
setEmail(event.currentTarget.value); | |
}; | |
return ( | |
<form onSubmit={handleSubmit} className={styles.EmailListForm}> | |
<h2>Subscribe to my email list!</h2> | |
<div className={styles.Wrapper}> | |
<input | |
placeholder="Email address" | |
name="email" | |
type="text" | |
onChange={handleEmailChange} | |
/> | |
<button type="submit">Subscribe</button> | |
</div> | |
</form> | |
); | |
}; | |
export default EmailListForm; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment